Vapor4 one to one relationship

I need to create a one to one relationship between 2 fluent entities but need some help as I don’t really understand how it works

I see there is new implmentation in vapor4 for @OptionalParent and @OptioonalChild. but I don’t really follow how this should be implemented with respect to both entities scheme and intiialisation and how it should be handled to access the relationship in the controller

In my models I have a User who has a single address.

Can anyone provide me an example of this following similar approach as how we handle other relationships in the book


What bits are you stuck on? A one-to-one relationship is a parent child relationship, which is covered in the book as a one-to-many. The only difference is that instead of using @Children you use @OptionalChild (so you might get an array that contains 0 or more children instead of a property where you might get a child if there is one). Make sense?

Thanks Tim - yes - and you helped me out on Discord so appreciate the help thank you.

1 Like