Javascript 是否允许sequelize foreignkey为空值?
如何允许foreignkey为null?我有一个Javascript 是否允许sequelize foreignkey为空值?,javascript,node.js,postgresql,sequelize.js,Javascript,Node.js,Postgresql,Sequelize.js,如何允许foreignkey为null?我有一个到和以下关联: Shelf belongsToMany(Book, { through: Library, as: "Books"}); Book belongsToMany(Shelf, { through: Library, as: "Shelves"}); Library section: DataTypes.STRING, // ... other fields 我想在库中的图书ID上记录一个null:
到和以下关联:
Shelf
belongsToMany(Book, { through: Library, as: "Books"});
Book
belongsToMany(Shelf, { through: Library, as: "Shelves"});
Library
section: DataTypes.STRING,
// ... other fields
我想在库
中的图书ID
上记录一个null
:
Library.create({
section: "blah",
bookId: null,
shelfId: 3
});
既然Sequelize自动在联接表库中创建bookId
和shelfId
,我该如何指定允许库中bookId
外键上的null
我在node.js
和sequelize.js
方面都不是专家,但在谷歌搜索一次,我就得到了答案。您可能需要注意文档中的这部分代码:
Library.hasMany(Picture, {
foreignKey: {
name: 'uid',
allowNull: false
}
});
似乎您需要在库中指定{foreignKey:{name:'bookId',allowNull:true}}
我尝试过,但它仍然拒绝允许null:{through:Library,as:“Books”,foreignKey:{name:'bookId',allowNull:true}
在“bookId”列中提供null值违反非空约束
是否尝试添加{foreignKey:{…},约束:false}
?注意:已批准@farhan yaseen的更改,因为我在复制的代码中有一些拼写错误。谢谢