Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Update OnetoOne.md #1310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion concepts/ORM/Associations/OnetoOne.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ There are currently two ways of handling this association in Waterline.

In this example, we are associating a `Pet` with a `User`. The `User` may only have one `Pet`, and a `Pet` can only have one `User`. However, in order to query from both sides in this example, we must add a `collection` attribute to the `User` model. This allows us to call both `User.find().populate('pet')` along with `Pet.find().populate('owner')`.

The two models will stay in sync by updating the `Pet` model's `owner` attribute. Adding the `unique` property ensures that only one value for each `owner` will exist in the database. The downside is that when populating from the `User` side, you will always get an array back.
The two models will stay in sync by updating the `Pet` model's `owner` attribute. Adding the `unique` property ensures that only one value for each `owner` will exist in the database. The downside is that when populating from the `User` side, you will always get an array back (`User.find().populate('pet')` will return `[{ pet: [] }]`).

Further explanation about:

> The two models will stay in sync by updating the `Pet` model's `owner` attribute.

If you look into the database, you will see that `pet` column does not exist in the `User` table. This is a virtual field calculated by Waterline. If you look at the `Pet` database, you will see the opposite, you will see a `owner` field there.

```javascript
// myApp/api/models/Pet.js
Expand Down