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.
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()
.
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.
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
, orthrough
properties reference models in either lowercase (the identity) or capitalized (the global ID). For example, in the following association, thecollection
property is set toproduct
—the identity of the Sails model calledProduct
:
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.