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.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Check out the full Sailsconf 2022 playlist on Youtube

Tweet Follow @sailsjs

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Concepts

  • Actions and controllers
    • Generating actions and controllers
    • Routing to actions
  • Assets
    • Default tasks
    • Disabling Grunt
    • Task automation
  • Blueprints
    • Blueprint actions
    • Blueprint routes
  • Configuration
    • The local.js file
    • Using `.sailsrc` files
  • Deployment
    • FAQ
    • Hosting
    • Scaling
  • E-commerce
  • Extending Sails
    • Adapters
      • Available adapters
      • Custom adapters
    • Custom responses
      • Adding a custom response
    • Generators
      • Available generators
      • Custom generators
    • Hooks
      • Available hooks
      • Events
      • Hook specification
        • .configure
        • .defaults
        • .initialize()
        • .registerActions()
        • .routes
      • Installable hooks
      • Project hooks
      • Using hooks
  • File uploads
    • Uploading to GridFS
    • Uploading to S3
  • Globals
    • Disabling globals
  • Helpers
    • Example helper
  • Internationalization
    • Locales
    • Translating dynamic content
  • Logging
    • Custom log messages
  • Middleware
    • Conventional defaults
  • Models and ORM
    • Associations
      • Many-to-many
      • One way association
      • One-to-many
      • One-to-one
      • Reflexive associations
      • Through associations
    • Attributes
    • Errors
    • Lifecycle callbacks
    • Model settings
    • Models
    • Query language
    • Records
    • Standalone Waterline usage
    • Validations
  • Policies
    • Access Control and Permissions
  • Programmatic usage
    • Tips and tricks
  • Realtime
    • Multi-server environments
    • On the client
    • On the server
  • Routes
    • Custom routes
    • URL slugs
  • Security
    • Clickjacking
    • Content security policy
    • CORS
    • CSRF
    • DDOS
    • P3P
    • Socket hijacking
    • Strict Transport Security
    • XSS
  • Services
  • Sessions
  • Shell scripts
  • Testing
  • Views
    • Layouts
    • Locals
    • Partials
    • View engines

Built with Love

The Sails framework is built by a web & mobile shop in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Community
  • News
  • For business
About:
  • Our company
  • Security
  • Legal
  • Logos/artwork
Help:
  • Get started
  • Documentation
  • Docs
  • Contribute
  • Take a class

© 2012-2022 The Sails Company. 
The Sails framework is free and open-source under the MIT License. 
Illustrations by Edamame.