【发布时间】:2019-01-07 16:39:37
【问题描述】:
vegetable.js
...
var Vegetable = sequelize.define('Vegetable', {
recipeId: { allowNull: false, ... },
name: { ... },
});
Vegetable.association = models => {
Vegetable.belongsTo(models.Recipe);
};
...
recipe.js
...
var Recipe = sequelize.define('Recipe', {
id: { ... },
name: { ... },
...
});
Recipe.association = models => {
Recipe.hasMany(models.Vegetable, {as: 'vegetables'});
};
Recipe.insert = recipe => {
const { Vegetable } = require('./vegetable');
return Recipe.create({
name: 'asdf',
vegetables: [{name: 'asdf'}],
...
}, {
include: [{
model: Vegetable,
as: vegetables',
}],
});
};
...
结果
SequelizeValidationError: notNull Violation: Vegetable.recipeId 不能为空
为什么 Vegetable.recipeId 没有用 Recipe.id 填充?
是否存在通过关联插入数据的最佳做法?
【问题讨论】:
标签: javascript node.js express sequelize.js