【发布时间】:2016-10-15 06:31:30
【问题描述】:
我有两个具有一对多关系的续集模型。我们称它们为所有者和财产。
假设它们是使用sails-hook-sequelize 定义的(简化)。
//Owner.js
module.exports = {
options: {
tableName: 'owner'
},
attributes: {
id: {
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING(255)
},
associations: function () {
Owner.hasMany(Property, {
foreignKey: {
name: 'owner_id'
}
});
}
}
//Property.js
module.exports = {
options: {
tableName: 'property'
},
attributes: {
id: {
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING(255)
}
}
现在假设我想在我的数据库中插入一个所有者记录并插入一些属性记录以与所有者关联。我该怎么做呢?
我正在寻找类似的东西
Owner.create({name:'nice owner',
property: [{name:'nice property'},
{name:'ugly property'}]});
令人惊讶的是,我在 Sequelize 文档中找不到这个。
【问题讨论】:
标签: javascript sql node.js orm sequelize.js