简体   繁体   中英

Sequelize: error of foreignKey constraint not reported when inserting new data?

I have table A one-to-many table B like below.

A.associate = function(models) {
    A.hasMany(models.B, {
        foreignKey: 'a_id',
        as: 'Bs'
    });
}

When I insert record B, I use a random a_id which does not exist in A. Therefore, I expect it to report error of foreign key constraint. But it doesn't report error, instead it adds B with a_id set to NULL.

Is there a setting to configure the model definition so that it can fail the operation and report error in such case?

Thanks.

ANSWER MYSELF

Use "allowNull: false" in foreignKey.

https://github.com/sequelize/sequelize/issues/2837

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM