admin管理员组文章数量:1315998
I am continuing to get the same error when I try to run seeds in Knex and Postgres. The error is error: insert or update on table "locations" violates foreign key constraint "locations_user_id_fore
ign"
. Can anyone figure out why it is throwing this error? I have tried changing a bunch of things. Thanks!
User Migrations
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')
};
Locations Table
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')
};
User Seeds
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'}
]);
});
};
Location Seeds
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}
]);
});
};
I am continuing to get the same error when I try to run seeds in Knex and Postgres. The error is error: insert or update on table "locations" violates foreign key constraint "locations_user_id_fore
ign"
. Can anyone figure out why it is throwing this error? I have tried changing a bunch of things. Thanks!
User Migrations
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')
};
Locations Table
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')
};
User Seeds
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'}
]);
});
};
Location Seeds
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}
]);
});
};
Share
Improve this question
asked May 27, 2017 at 3:08
lnambalnamba
1,7213 gold badges20 silver badges26 bronze badges
1 Answer
Reset to default 8Putting as answer, as it would be too big for a ment.
Well the error message is clear enough that you have violated a foreign key
constraint.
This is because for inserting a row in locations
you need to give a user_id
as well, which is referencing to column id
in table users
. If that particular user is not present in users
table, you cannot insert that user_id
in locations. See this line
table.integer('user_id').notNullable().references('id').inTable('users').onDelete('cascade');
So first you have to add that user
in users
table, then you can insert it it location
.
本文标签:
版权声明:本文标题:javascript - PostgresKnex "insert or update on table "locations" violates foreign key constraint& 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741986308a2408700.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论