table 上的插入或更新违反了外键约束 PSQL/Knex
Insert or update on table violates foreign key constraint PSQL/Knex
这是我的第一个 psql 数据库。我正在使用 knex。
我有 3 个 table:用户、users_posts 和 users_comments。我想对其进行设置,以便用户可以 post 事物,还可以对其他用户发表评论 post。
当我为 users_comments 播种时,出现此错误:
insert or update on table "users_comments" violates foreign key constraint
如何修改我的 tables 以使 users_comment table 接受外部 post_id 密钥?还是有更好的方法让我设置 post 和用户的评论?
用户table
table.increments();
table.string('username').notNullable().defaultTo('').unique();
table.string('email').notNullable().unique();
table.specificType('hashed_password', 'char(60)').notNullable();
table.timestamps(true, true);
users_posts table
table.increments();
table.integer('user_id')
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE')
.index();
table.string('post_title').notNullable().defaultTo('');
table.string('post_content').notNullable().defaultTo('');
table.timestamps(true, true);
users_comments table
table.increments();
table.integer('user_id')
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE')
.index();
table.integer('post_id')
.notNullable()
.references('id')
.inTable('users_posts')
.onDelete('CASCADE')
.index();
table.string('comment_content').notNullable().defaultTo('');
table.timestamps(true, true);
users_comments种子:
id: 1,
user_id: 1,
post_id: 1,
comment_content:"...",
created_at: new Date('2016-06-29 14:26:16 UTC'),
updated_at: new Date('2016-06-29 14:26:16 UTC')
成功了。对 user_comments 进行了这些更改 ...
table.integer('user_id')
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE')
.index();
table.integer('id')
.notNullable()
.references('user_posts')
.onDelete('CASCADE')
.index();
table.string('comment_content').notNullable().defaultTo('');
table.timestamps(true, true);
这是我的第一个 psql 数据库。我正在使用 knex。
我有 3 个 table:用户、users_posts 和 users_comments。我想对其进行设置,以便用户可以 post 事物,还可以对其他用户发表评论 post。
当我为 users_comments 播种时,出现此错误:
insert or update on table "users_comments" violates foreign key constraint
如何修改我的 tables 以使 users_comment table 接受外部 post_id 密钥?还是有更好的方法让我设置 post 和用户的评论?
用户table
table.increments();
table.string('username').notNullable().defaultTo('').unique();
table.string('email').notNullable().unique();
table.specificType('hashed_password', 'char(60)').notNullable();
table.timestamps(true, true);
users_posts table
table.increments();
table.integer('user_id')
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE')
.index();
table.string('post_title').notNullable().defaultTo('');
table.string('post_content').notNullable().defaultTo('');
table.timestamps(true, true);
users_comments table
table.increments();
table.integer('user_id')
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE')
.index();
table.integer('post_id')
.notNullable()
.references('id')
.inTable('users_posts')
.onDelete('CASCADE')
.index();
table.string('comment_content').notNullable().defaultTo('');
table.timestamps(true, true);
users_comments种子:
id: 1,
user_id: 1,
post_id: 1,
comment_content:"...",
created_at: new Date('2016-06-29 14:26:16 UTC'),
updated_at: new Date('2016-06-29 14:26:16 UTC')
成功了。对 user_comments 进行了这些更改 ...
table.integer('user_id')
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE')
.index();
table.integer('id')
.notNullable()
.references('user_posts')
.onDelete('CASCADE')
.index();
table.string('comment_content').notNullable().defaultTo('');
table.timestamps(true, true);