Reputation: 33
Faced serious problems here with Sails Js. So, i have one-to-many relationship as described below (taken from official sails documentation):
// myApp/api/models/User.js
// A user may have many pets
module.exports = {
attributes: {
firstName: {
type: 'string',
columnName: "first_name"
},
lastName: {
type: 'string',
columnName: "last_name"
},
// Add a reference to Pets
pets: {
collection: 'pet',
via: 'owner'
}
}
};
and dependent model:
// myApp/api/models/Pet.js
// A pet may only belong to a single user
module.exports = {
attributes: {
breed: {
type: 'string'
//column name will be 'breed' by default
},
typeProperty: {
type: 'string',
columnName: "type_property"
},
nameProperty: {
type: 'string',
columnName: "name_property"
},
// Add a reference to User
owner: {
model: 'user'
}
}
};
When I'm calling in the code following query
User.find()
.populate('pets')
.exec(function(err, users) {
if(err) // handle error
// The users object would look something like the following
[{
id: 123,
firstName: 'Foo',
lastName: 'Bar',
pets: [{
id: 1,
// !! Only this property is loaded !!
breed: 'labrador',
// !! This property is NOT loaded !!
typeProperty: undefined,
// !! This property is NOT loaded !!
nameProperty: undefined,
user: 123
}]
}]
});
Basically, seems that sails (waterline if to be specific) is not mapping back properties, which have custom "columName" specified and differs from property name (ex. "typeProperty" stored in type_property
column).
Has anyone faced this kind of problem?
Upvotes: 2
Views: 632
Reputation: 1631
In fact, I faced this problema. The property "columnName" is not working. Seems like Sails doesn't prioritizes this property over it's model's convention naming.
Try to change the model attribute name to be equal your database property.
type_property: {
type: 'string'
},
this should make your attributes to be populated. Worked here.
Column name works fine when the attribute in question is a foreign key.
Upvotes: 1