对表进行PSQL / Knex插入或更新违反了外键约束

时间:2019-06-30 13:52:14

标签: node.js postgresql knex.js

在节点/表达服务器中使用knex迁移和播种我的postgreSQL数据库。进行迁移,在SQLite3中,将数据植入种子没有问题,但是当我将其转换为PostgreSQL数据库时,在用户ID上出现了FK约束错误。我已经在json文件中以编程方式和手动方式生成了种子数据,并且仍然存在相同的错误。

Error: Error while executing "/Users/andre/projects/React-native/Lambda-Social-App/server/data/seeds/002-Subtopics.js" seed: insert into "subtopic" ("creater_id", "title") values ($1, $2), ($3, $4), ($5, $6), ($7, $8), ($9, $10), ($11, $12), ($13, $14), ($15, $16), ($17, $18), ($19, $20), ($21, $22), ($23, $24), ($25, $26), ($27, $28), ($29, $30), ($31, $32), ($33, $34), ($35, $36), ($37, $38), ($39, $40) - insert or update on table "subtopic" violates foreign key constraint "subtopic_creater_id_foreign" at _bluebird.default.resolve.then.then.catch.originalError (/Users/andre/projects/React-native/Lambda-Social-App/node_modules/knex/lib/seed/Seeder.js:137:21) error: insert or update on table "subtopic" violates foreign key constraint "subtopic_creater_id_foreign"

User.js

    exports.up = function (knex, Promise) {
  return knex.schema.createTable('users', user => {

    user.string('id').unique().primary().notNullable();
    user.string('username', 25).notNullable();
    user.string('email', 128).unique().notNullable();
    user.string('avatar', 100).notNullable();
    user.timestamps(true, true);
  });
};

Subtopic.js

exports.up = function (knex, Promise) {
  return knex.schema.createTable('subtopic', subtopic => {

    subtopic.increments('id').primary().notNullable();
    subtopic.string('title', 50).unique().notNullable();
    subtopic.timestamp(true, true);
    subtopic.string('creator_id').notNullable().references('id').inTable('users').onDelete('CASCADE').onUpdate('CASCADE');
  });
};

0 个答案:

没有答案