允许sequelize foreignkey的空值?

时间:2015-02-09 01:22:10

标签: javascript node.js postgresql sequelize.js

如何为外键允许null?我有throughbelongsToMany关联:

Shelf
    belongsToMany(Book, { through: Library, as: "Books"});

Book
    belongsToMany(Shelf, { through: Library, as: "Shelves"});

Library
    section: DataTypes.STRING,
    // ... other fields

我想在null的{​​{1}}上记录bookId

Library

由于Sequelize会自动在联接表Library.create({ section: "blah", bookId: null, shelfId: 3 }); 中创建bookIdshelfId,我如何指定我要在Library上允许null } {foreign} in bookId

1 个答案:

答案 0 :(得分:6)

我不是node.js也不是sequelize.js的专家,但是在Google中进行了一次搜索我获得了official documentation。您可能需要注意文档中这部分代码:

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

您似乎需要在{foreignKey: {name: 'bookId', allowNull: true} }中指定Library