Edit Page

Associations

Overview

#

In addition to being literal types like string and number, attributes in a Sails model can represent links to other records in a datastore. Attributes of this type are called associations. For example, given a User model and a Pet model, the User may contain a pets attribute that links a given user to one or more pets.

Setting values for associations
#

Depending on the type of link, an association attribute can be set in a .create() or .update() call by giving it the value of another record’s primary key, or by using special model methods like .addToCollection, .removeFromCollection(), or .replaceCollection().

Associations in retrieved records
#

Unlike normal attributes, association attribute values are not always returned when retrieving a record with .find() or .findOne(). Instead, you declare which associations to retrieve by using the .populate() method:

// Find a single user, including its pets
var userWithPets = await User.findOne(123).populate('pets');

How an association attribute is represented in a returned record depends on the type of association, whether there are actual records linked, and whether .populate() is chained to the query. See this table for a full description of what to expect in a returned record with association attributes.

Cross-adapter associations

#

With Sails and Waterline, you can associate models across multiple data stores. This means that even if your users live in PostgreSQL and their comments live in MongoDB, you can interact with the data as if they lived together in the same database. You can also have associations that span different datastores using the same adapter. This comes in handy if, for example, your app needs to access/update legacy recipe data stored in a MySQL database somewhere in your company's data center, but also store/retrieve ingredient data from a brand new MySQL database in the cloud.

IMPORTANT NOTE

In tutorials and example code, you might sometimes see associations' collection, model, or through properties reference models in either lowercase (the identity) or capitalized (the global ID). For example, in the following association, the collection property is set to product—the identity of the Sails model called Product:

wishlist: {
 collection: 'product',
 via: 'wishlistedBy'
}

In the Sails docs, we always use the global ID approach for consistency's sake. But realize that either approach will work.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Concepts