允许空值用于后缀foreignkey?
我如何让一个外键为空? 我有一个through
和belongsToMany
协会:
Shelf belongsToMany(Book, { through: Library, as: "Books"}); Book belongsToMany(Shelf, { through: Library, as: "Shelves"}); Library section: DataTypes.STRING, // ... other fields
我想在Library
的bookId
上logging一个null:
Library.create({ section: "blah", bookId: null, shelfId: 3 });
由于Sequelize会自动在连接表Library
创buildbookId
和shelfId
,我将如何指定我希望允许Library
的bookId
键为null
?
我不是node.js
专家,也没有sequelize.js
,但在谷歌search我有官方文档 。 您可能需要注意文档中的这部分代码:
Library.hasMany(Picture, { foreignKey: { name: 'uid' allowNull: false } })
看来你需要在库中指定{foreignKey: {name: 'bookId', allowNull: TRUE} }
。