Edit Page

Reflexive associations

Overview

In most cases, an association will be between attributes of two different models—for example, a relationship between a User model and a Pet model. However, it is also possible to have a relationship between two attributes in the same model. This is called a reflexive association.

Consider the following User model:

// myApp/api/models/User.js
module.exports = {
  attributes: {
    firstName: {
      type: 'string'
    },
    lastName: {
      type: 'string'
    },

    // Add a singular reflexive association
    bestFriend: {
      model: 'user',
    },

    // Add one side of a plural reflexive association
    parents: {
      collection: 'user',
      via: 'children'
    },

    // Add the other side of a plural reflexive association
    children: {
      collection: 'user',
      via: 'parents'
    },

    // Add another plural reflexive association, this one via-less
    bookmarkedUsers: {
      collection: 'user'
    }

  }
};

The reflexive associations in the example User model above operate just like any other associations. The singular bestFriend attribute can be set to the primary key of another user (or for the narcissistic, to the same user!). The parents and children attributes can be modified using .addToCollection(), .removeFromCollection() and .replaceCollection(). Note that as with all plural associations, adding to one side will allow the relationship to be accessed by either side, so running:

// Add User #12 as a parent of User #23
await User.addToCollection(23, 'parents', 12);
// Find User #12 and populate its children
var userTwelve = await User.findOne(12).populate('children');

would return something like:

{
  id: 12,
  firstName: 'John',
  lastName: 'Doe',
  bestFriend: null,
  children: [
    {
      id: 23,
      firstName: 'Jane',
      lastName: 'Doe',
      bestFriend: null
    }
  ]
}

Notes

As with all “via-less” plural associations, reflexive via-less associations are only accessible from the side on which they are declared. In the above User model, you can do User.findOne(55).populate('bookmarkedUsers') to find all of the users that User #55 bookmarked, but there’s no way to get a list of all of the users that have bookmarked User #55. To do so would require an additional attribute (e.g. bookmarkedBy) that would be joined to bookmarkedUsers using the via property.

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.

Sailsconf 2022 June 22 - 24: Learn more at the Sailsconf website

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-2021 The Sails Company. 
The Sails framework is free and open-source under the MIT License. 
Illustrations by Edamame.