简体   繁体   English

Sequelize:插入新数据时未报告foreignKey约束错误?

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

I have table A one-to-many table B like below. 我有下表A的一对多表B。

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. 当我插入记录B时,我使用A中不存在的随机a_id。因此,我希望它报告外键约束的错误。 But it doesn't report error, instead it adds B with a_id set to NULL. 但是它不会报告错误,而是将a_id设置为NULL的B添加。

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. 在foreignKey中使用“ allowNull:false”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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