allow null value for sequelize foreignkey? allow null value for sequelize foreignkey? postgresql postgresql

allow null value for sequelize foreignkey?


I am no expert on node.js nor sequelize.js, but with one search in google I got the official documentation. You might need to pay some attention to this part of the code in the documentation:

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

It seems you need to specify the {foreignKey: {name: 'bookId', allowNull: true} } in Library.


By default, the Sequelize association is considered optional. If you would like to make the foreign key required, you can set allowNull to be false.

Foo.hasOne(Bar, {  foreignKey: {    allowNull: false  }});

See the docs