SequelizeForeignKeyConstraintError: insert or update on table "states" violates foreign key constraint "states_country_id_fkey"
SequelizeForeignKeyConstraintError: insert or update on table "states" violates foreign key constraint "states_country_id_fkey"
我正在尝试使用 Sequelize Sync 功能插入数据。但我收到了这个错误。问题是有时数据成功输入有时没有
错误
config/sequelize.js:295
SequelizeForeignKeyConstraintError: insert or update on table "states" violates foreign key
constraint "states_country_id_fkey"
at Query.formatError
(d:\projects\petrolpump\node_modules\sequelize\lib\dialects\postgres\query.js:315:16)
at Query.run (d:\projects\petrolpump\node_modules\sequelize\lib\dialects\postgres\query.js:87:18)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {name:
'SequelizeForeignKeyConstraintError', parent: error: insert or update on table "states" viol…ign key
constraint "states_country_id_fkey"
…, original: error: insert or update on table "states" vio…gn key constraint "states_country_id_fkey"
…, sql: 'INSERT INTO "states" ("id","name","country_id"…
untry_id","is_active","createdAt","updatedAt";', parameters: Array(5), …}
这是查询
sync function initial() {
try {
var country = await db.country.findOne({ where: { name: 'India' } });
if (!country) {
db.country.create({
name: "U.S.",
is_active: "true"
});
}
var state = await db.state.findOne({ where: { name: 'Gujarat' } });
if (!state) {
await db.state.create({
name: "California",
country_id: "1",
is_active: "true"
});
}
} catch (e) {
console.log(e)
}
}
db.sequelize.sync({ force: true }).then(() => {
console.log("Drop and re-sync db.");
initial();
});
这是协会
db.state.hasMany(db.city, {
foreignKey: 'state_id'
});
db.country.hasMany(db.state, {
foreignKey: 'country_id',
});
我曾尝试在模型文件中使用参考,但出现了合理的错误
我能够解决这个错误。我不知道这是因为版本的错误,但是当我安装了适用于我的旧版本时,它是 5.21.3
我在使用 NodeJS 和 Sequelize 构建 API 时也遇到了这个错误。这是我解决问题的方法。
错误:
SequelizeForeignKeyConstraintError:在 table“states”上插入或更新违反了外键
约束“states_country_id_fkey”
从上面的错误来看,状态table需要country_id作为它的foreignKey。也就是说,如果国家 table.
中没有指定的 country_id,您将无法创建州
解决方案:
我建议首先播种“国家”table,以确保存在“州”table 中引用的指定 country_id。
然后尝试将数据插入状态 table。那应该可以解决问题。
我正在尝试使用 Sequelize Sync 功能插入数据。但我收到了这个错误。问题是有时数据成功输入有时没有
错误
config/sequelize.js:295
SequelizeForeignKeyConstraintError: insert or update on table "states" violates foreign key
constraint "states_country_id_fkey"
at Query.formatError
(d:\projects\petrolpump\node_modules\sequelize\lib\dialects\postgres\query.js:315:16)
at Query.run (d:\projects\petrolpump\node_modules\sequelize\lib\dialects\postgres\query.js:87:18)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {name:
'SequelizeForeignKeyConstraintError', parent: error: insert or update on table "states" viol…ign key
constraint "states_country_id_fkey"
…, original: error: insert or update on table "states" vio…gn key constraint "states_country_id_fkey"
…, sql: 'INSERT INTO "states" ("id","name","country_id"…
untry_id","is_active","createdAt","updatedAt";', parameters: Array(5), …}
这是查询
sync function initial() {
try {
var country = await db.country.findOne({ where: { name: 'India' } });
if (!country) {
db.country.create({
name: "U.S.",
is_active: "true"
});
}
var state = await db.state.findOne({ where: { name: 'Gujarat' } });
if (!state) {
await db.state.create({
name: "California",
country_id: "1",
is_active: "true"
});
}
} catch (e) {
console.log(e)
}
}
db.sequelize.sync({ force: true }).then(() => {
console.log("Drop and re-sync db.");
initial();
});
这是协会
db.state.hasMany(db.city, {
foreignKey: 'state_id'
});
db.country.hasMany(db.state, {
foreignKey: 'country_id',
});
我曾尝试在模型文件中使用参考,但出现了合理的错误
我能够解决这个错误。我不知道这是因为版本的错误,但是当我安装了适用于我的旧版本时,它是 5.21.3
我在使用 NodeJS 和 Sequelize 构建 API 时也遇到了这个错误。这是我解决问题的方法。
错误: SequelizeForeignKeyConstraintError:在 table“states”上插入或更新违反了外键 约束“states_country_id_fkey”
从上面的错误来看,状态table需要country_id作为它的foreignKey。也就是说,如果国家 table.
中没有指定的 country_id,您将无法创建州解决方案: 我建议首先播种“国家”table,以确保存在“州”table 中引用的指定 country_id。
然后尝试将数据插入状态 table。那应该可以解决问题。