允许空值用于后缀foreignkey?

我如何让一个外键为空? 我有一个throughbelongsToMany协会:

 Shelf belongsToMany(Book, { through: Library, as: "Books"}); Book belongsToMany(Shelf, { through: Library, as: "Shelves"}); Library section: DataTypes.STRING, // ... other fields 

我想在LibrarybookId上logging一个null:

 Library.create({ section: "blah", bookId: null, shelfId: 3 }); 

由于Sequelize会自动在连接表Library创buildbookIdshelfId ,我将如何指定我希望允许LibrarybookId键为null

我不是node.js专家,也没有sequelize.js ,但在谷歌search我有官方文档 。 您可能需要注意文档中的这部分代码:

 Library.hasMany(Picture, { foreignKey: { name: 'uid' allowNull: false } }) 

看来你需要在库中指定{foreignKey: {name: 'bookId', allowNull: TRUE} }