1

I'm struggling upon a problem related to Sails.js and Waterline associations. The database technology is mongoDB and I'm using the sails-mongo adapter. I have a model User which has an association with a Role model. The association is of one-to-many type. The association is declared as follows:

/** -------------------------
* User.js
* ---------------------------
*/
{...}
role: {
  model: 'Role'
},
{...}


/** -------------------------
* Role.js
* ---------------------------
*/
{...}
users: {
  collection: 'Role',
  via: 'role'
},
{...}

When I make a query to retrieve all the users and I populate the role attribute, I have the role attribute populated with the first role inserted in the database even for those users that don't have any role associated.

Anyone had encountered this problem? Thanks a lot.

mastergap
  • 375
  • 1
  • 3
  • 10
  • Today I've tried to switch to a MySQL database and everything is ok. If I call a find populating associations if there are no associations collection attributes are empty. It seems a problem with the sails-mongo adapter. – mastergap Aug 29 '15 at 08:59

1 Answers1

1

Use this

role: {
  model: 'Role',
  defaultsTo: null
},

I also think it's sails-mongo bug.

Andi N. Dirgantara
  • 2,050
  • 13
  • 20