Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否允许sequelize foreignkey为空值?_Javascript_Node.js_Postgresql_Sequelize.js - Fatal编程技术网

Javascript 是否允许sequelize foreignkey为空值?

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:

如何允许foreignkey为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的更改,因为我在复制的代码中有一些拼写错误。谢谢