Reputation: 33
i follow this link and trying to show the data
http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-way-association
but i got the error "Unknown column 'shop.building_detail' in 'field list' "
Is it the sails bug or something i did wrong ?
there are my database design and my code in below
Shop model:
module.exports = {
autoPK:false,
attributes : {
shop_id : {
primaryKey : true,
type : 'int',
unique: true,
columnName : 'shop_id'
},
shop_floor : {
type : 'string'
},
shop_room : {
type : 'string'
},
shop_build : {
type : 'int',
foreignKey:'true'
},
building_detail : {
model : 'building'
}
}
};
Building model:
module.exports = {
autoPK:false,
attributes : {
build_id : {
primaryKey : true,
type : 'int',
unique: true,
columnName : 'build_id'
},
build_name : {
type : 'string'
},
build_address : {
type : 'string'
},
build_latitude : {
type : 'float'
},
build_longitude : {
type : 'float'
},
build_visit_count : {
type : 'int'
},
build_status : {
type : 'string'
},
build_status_last_update_time : {
type : 'time'
}
}
};
Upvotes: 0
Views: 393
Reputation: 3536
With your database design, your Shop
model could look like this:
Shop.js
module.exports = {
autoPK:false,
attributes : {
shop_id : {
primaryKey : true,
type : 'int',
unique: true,
columnName : 'shop_id'
},
shop_floor : {
type : 'string'
},
shop_room : {
type : 'string'
},
shop_build : {
model: 'Building'
}
}
};
Sails.js automatically associates shop_build
with the primary key of the Building
model.
When you create a shop, just specify the building id as the value for shop_build
:
Shop.create({
shop_floor: "Some floor",
shop_room: "Some room",
shop_build: 8 // <-- building with build_id 8
})
.exec(function(err, shop) { });
And when you perform shop queries, you can populate the building details:
Shop.find()
.populate('shop_build')
.exec(function(err, shops) {
// Example result:
// [{
// shop_id: 1,
// shop_floor: 'Some floor',
// shop_room: 'Some room',
// shop_build: {
// build_id: 8,
// build_name: 'Some building',
// ...
// }
// }]
});
See Waterline documentation on associations for more details.
Upvotes: 1