sequelize-foreignkey允许空值
allow null value for sequelize foreignkey?
如何允许外键为null?我有一个through
和belongsToMany
关联:
Shelf
belongsToMany(Book, { through: Library, as: "Books"});
Book
belongsToMany(Shelf, { through: Library, as: "Shelves"});
Library
section: DataTypes.STRING,
// ... other fields
我想在Library
:中的bookId
上记录一个null
Library.create({
section: "blah",
bookId: null,
shelfId: 3
});
由于Sequelize会自动在联接表Library
中创建bookId
和shelfId
,我如何指定我希望在Library
中的bookId
外键上允许null
?
我既不是node.js
也不是sequelize.js
的专家,但在谷歌上搜索一下,我就得到了官方文档。您可能需要注意文档中的这部分代码:
Library.hasMany(Picture, {
foreignKey: {
name: 'uid',
allowNull: false
}
});
似乎您需要在Library
中指定{foreignKey: {name: 'bookId', allowNull: true} }
。
默认情况下,Sequelize关联被认为是可选的。如果您想使外键成为必需的,可以将allowNull
设置为false
。
Foo.hasOne(Bar, {
foreignKey: {
allowNull: false
}
});
请参阅文档
相关文章:
- 如果值为空,如何设置输入的默认值?jQuery
- React 15使用空字符串删除输入值
- 如果用户没有更改,则表单值为空
- 如果值为空或0,如何更改html select标记的边框颜色
- jQuery如果值为空,则将其设置为0并进行验证
- PHP一直告诉我值为空
- AngularJS dropdownlist的第一个值为空
- 我可以让高图表中的数据值为空吗
- 如果值为空,则从对象中删除项
- if子句中的值为空
- ASP.NET 从 JQuery 设置时,服务器端的文本框值为空
- 挖空 + 选择2 -- 设置默认值
- 将挖空单选按钮值的值存储在可观察数组中
- 柴 js 期望属性值为空数组
- 文本区域和 iframe 值为空
- 如果使用 jquery 值为空,则无法为占位符设置值属性
- 如果值为空,ng-model 将从对象中删除元素
- 挖空:属性中的值:标签未回收
- 通过jquery在数据表中设置“搜索框”的值为空
- 对于 ajax GET,值为空