我有一个与笛卡儿一起回来的查询,因为其中一个表连接可以有很多结果。用例是我有一家公司可以有很多地方。
以下是我在查询中收到的回复:
month
我最终想把它变成:
[
{
// this is the company info
oid: 31,
companyName: 'Foo Bar Inc',
avatar: 'http://placehold.it/100x100',
bannerImage: 'http://placehold.it/800x200',
// here starts the location
name: 'Foo Bar Port',
address: '1234 West 31 Street',
city: 'New York',
state: 'New York'
},
{
oid: 31,
companyName: 'Foo Bar Inc',
avatar: 'http://placehold.it/100x100',
bannerImage: 'http://placehold.it/800x200',
name: 'Foo Bar Warehouse',
address: '644 Main Street',
city: 'Los Angeles',
state: 'California'
}
]
处理此问题的最佳方法是什么?
答案 0 :(得分:0)
以下示例如果您在node.js中使用Sequelize ORM:
此处公司和位置是模型。
// Company.js
// ...
Company.associate = function(models) {
// Using additional options like CASCADE etc for demonstration
// Can also simply do Company.belongsTo(models.Location);
Company.belongsTo(models.Location, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
// ...