Vapor 4 and Fluent: Does it support "Owns Destination" and "Propagate Primary Key"?


I want to model a one-to-one relationship.

Back in the days when I was still using Apple WebObjects we did this by ticking the appropriate Checkboxes in EOModeller. The effect was that the child table used the same primary key as the parent table and that the child could not exist without it's parent. A good example of why to do it this way is detailed here:'t_understand_"propagates_primary_key"#What.27s_it_good_for .

Is there a way to do it like this in Fluent, e.g. having the parent's primary key to be propagated to the child's primary key?

For those, who don't know WebObjects and the Enterprise Objects Framework (short "EOF") here is a PDF for further studies:



If I understand the question (and I have no experience of WebObjects so it's highly likely!) you want to set up a foreign key constant on the child table. Which is definitely possible in Fluent, like so. That will stop you saving a child if the parent doesn't exist in the db

Hi Tim,

the example you gave me looks so different from what is detailed here: , so at first one question: is that really Vapor 4 code or just another way to do things?

What I want to accomplish is to have referential integrity at database level: the child table should not be able to contain more than one entry for each parent table entry. When modelling this at the database level you'll propagate the primary key of the parent table to the child table as primary key (and use it as foreign key at the same time). If I understand the example you have given correctly it would indeed be possible for more than one Acronym to share the same userID since those Acronyms are identified by their id. So, theoretically, it is possible to have more than one Acronym with the same userID.

Of course there are ways to avoid such situations by handling those in the Swift code. I guess you where trying to show me such an example. But I must confess I don't see how such a situation is avoided there. This might be due to the fact that I am pretty new to Vapor (4, don't know the other versions at all) and so don't understand what is going on there (for instance, what is that:

  var categories: Siblings<Acronym, Category, AcronymCategoryPivot> {
    return siblings()

doing there? what is it for?).

I hope I explained sufficiently enough what my goal is (I am really bad at explaining, sorry).

One last thing: an unsolicited advice (I know, people hat those, but his one is here really trying to help):

If you're going to implemtent an ORM, make yourself familiar with the Enterprise Objects Framework (EOF).

It is one of the oldest and maybe still one of the best ORMs out there, battle testest and very mature, although not longer supported by Apple. But still available here in the form of Java jars as part of WebObjects 5.4.3: (I know that doesn't help that much but you could at least look at the APIs of those jars — I hope you don't illegally decompile it with tools like Intellij!) or have a look at an open source implementation of the EOF (I know, it is written in Objective-C, a language disliked by many here, but at least you can snatch some ideas from it). EOF has a lot of great stuff inside (despite being so old) and it is really a shame, that other ORMs learned so less from it (I am looking at Hibernate here …)

Kind regards,