Allow null value for secelize foreignkey?

How to allow null for a foreign key? I have an association throughand belongsToMany:

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

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

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

I would like to write zero in bookIdin Library:

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

Since Sequelize automatically creates bookIdand shelfIdin the connection table Library, as if I said that I want to allow nullin the bookIdfile foreignkey in Library?

+4
source share
1 answer

I am not an expert on node.jsand sequelize.js, but with one google search I got the official documentation. You may need to pay attention to this part of the code in the documentation:

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

, {foreignKey: {name: 'bookId', allowNull: TRUE} } Library.

+5

All Articles