KnexとPostgresでシードを実行しようとすると、同じエラーが発生します。エラーはerror: insert or update on table "locations" violates foreign key constraint "locations_user_id_fore ign"
です。なぜ誰がこのエラーを投げているのか理解できますか?私はたくさんのものを変えようとしました。ありがとう!Postgres/Knex "テーブルの"場所の挿入または更新は "外部キー制約に違反しています"
ユーザー移行
exports.up = function(knex, Promise) {
return knex.schema.createTable('users', function(table) {
table.increments()
table.string('email').notNullable();
table.string('password').notNullable();
})
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('users')
};
場所の表
exports.up = function(knex, Promise) {
return knex.schema.createTable('locations', function(table) {
table.increments('id');
table.string('placename').notNullable();
table.float('latitude').notNullable();
table.float('longitude').notNullable();
table.integer('user_id').notNullable().references('id').inTable('users').onDelete('cascade');
})
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('locations')
};
ユーザー種子
exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('users').del()
.then(function() {
// Inserts seed entries
return knex('users').insert([
{email: '[email protected]', password: 'p1'},
{email: '[email protected]', password: 'p2'},
{email: '[email protected]', password: 'p3'}
]);
});
};
場所種子
exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('locations').del()
.then(function() {
// Inserts seed entries
return knex('locations').insert([
{placename: 'Himeji', latitude: 34.8394, longitude: 134.6939, user_id: 1},
{placename: 'Long Beach', latitude: 33.7701, longitude: 118.1937, user_id: 3},
{placename: 'Seattle', latitude: 47.6253, longitude: 122.3222, user_id: 2}
]);
});
};
ありがとう、ええ、私は種子が順不同であることに気づいた。だから私はユーザーを作成する前に、ユーザーを参照する外部キーを持つ場所を作成しようとしていました。ありがとう! – lnamba