我已经在sequelize中定义了两个模型,如此
// Define models
var Device = connection.define('Device', {
name: Sequelize.STRING,
status: Sequelize.STRING,
os: Sequelize.STRING
})
var User = connection.define('User', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
name: Sequelize.STRING,
department: Sequelize.STRING
})
Device.belongsTo(User)
User.hasMany(Device)
现在我正试图在数据库中创建一些种子数据
// Insert seeds data into database
connection
.sync({ force: true })
.then(function () {
Device.bulkCreate(seeds.devices)
User.bulkCreate(seeds.users)
})
我已将种子数据定义为不同文件中的两个数组对象,如此,
var users = [
{
id: 1,
name: 'UserOne',
department: 'Department One'
},
{
id: 2,
name: 'UserTwo',
department: 'Department Two'
}
]
var devices = [
{
name: 'Galaxy S7',
status: 'Caged',
os: 'Android',
UserDevices: 1
},
{
name: 'iPhone 7',
status: 'Borrowed',
os: 'iOS'
}
]
有没有办法可以将设备与种子数据中的用户表关联,只需在属性中声明它,就像这样?
{
name: 'iPhone 7',
status: 'Borrowed',
os: 'iOS',
UserId: 1
}