用户表:
var user = connection.define('user', {
id: {
type: sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: sequelize.STRING
},
age: {
type: sequelize.INTEGER
}
}, {
classMethods: {
insertData: function(sql, data) {
this.create({
name: data.name,
age: data.age
}).then(function() {
console.log("successful insertion");
});
}
}
});
地址表:
var address = connection.define('address', {
id: {
type: sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
content: sequelize.TEXT
}, {
classMethods: {
associate: function(model) {
var user = model.user;
user.hasMany(address);
},
insertData: function(data) {
this.create({
content: data.content,
userId: data.id
}).then(function() {
console.log("working ");
});
}
}
});
现在,我如何在用户和地址之间的关系之后创建的表地址的字段userId
中插入数据。
直接插入如下所述的字段名称不起作用。
insertData: function(data) {
this.create({
content: data.content,
userId: data.id
}).then(function() {
console.log("working ");
});
}