我不确定这似乎是什么问题,但是此knex迁移失败。即使我不熟悉编写迁移,我也坚信此迁移文件是正确的。产生的错误如下
migration file "20190321113401_initial.js" failed
migration failed with error: alter table "meraki"."role_permissions" add constraint "role_permissions_role_id_foreign" foreign key ("role_id") references "roles" ("id") - relation "roles" does not exist
代码如下。最初,这些迁移功能位于单独的文件中,然后我以为是失败的,因为这些文件没有同步执行,这导致我编写了一个文件。我不确定这是否有帮助,但是当我删除包含外键引用(UserRoles,RolePermissions,Tokens)的表的代码时,其余部分似乎仍然有效。
'use strict';
exports.up = async knex => {
// Create Schema
await knex.raw('CREATE SCHEMA IF NOT EXISTS meraki');
// Load Extensions
await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
// Roles
await knex.schema.withSchema('meraki').createTable('roles', table => {
table
.string('id')
.primary()
.defaultTo(knex.raw('uuid_generate_v4()'));
table
.string('name')
.unique()
.notNullable();
table.string('description').notNullable();
table.timestamps();
});
// Permissions
await knex.schema.withSchema('meraki').createTable('permissions', table => {
table
.string('id')
.primary()
.defaultTo(knex.raw('uuid_generate_v4()'));
table
.string('name')
.unique()
.notNullable();
table.timestamps();
});
// Role Permissions
await knex.schema.withSchema('meraki').createTable('role_permissions', table => {
table
.string('role_id')
.notNullable()
.references('id')
.inTable('roles');
table
.string('permission_id')
.notNullable()
.references('id')
.inTable('permissions');
table.timestamps();
});
// Users
await knex.schema.withSchema('meraki').createTable('users', table => {
table
.string('id')
.primary()
.defaultTo(knex.raw('uuid_generate_v4()'));
table
.string('email', 320)
.unique()
.notNullable();
table.string('first_name', 35).notNullable();
table.string('middle_name', 35).notNullable();
table.string('last_name', 35).notNullable();
table.boolean('email_verified');
table.string('verification_token');
table.timestamps();
});
// User Roles
await knex.schema.withSchema('meraki').createTable('user_roles', table => {
table
.string('user_id')
.notNullable()
.references('id')
.inTable('users');
table
.string('role_id')
.notNullable()
.references('id')
.inTable('roles');
table.timestamps();
});
// Tokens
await knex.schema.withSchema('meraki').createTable('tokens', table => {
table.string('id').primary();
table
.string('user_id')
.notNullable()
.references('id')
.inTable('users');
table
.bigInteger('ttl')
.notNullable()
.defaultTo(1209600);
table.timestamps();
});
};
exports.down = async knex => {
// Tokens
await knex.schema.withSchema('meraki').dropTableIfExists('tokens');
// User Roles
await knex.schema.withSchema('meraki').dropTableIfExists('user_roles');
// Users
await knex.schema.withSchema('meraki').dropTableIfExists('users');
// Role Permissions
await knex.schema.withSchema('meraki').dropTableIfExists('role_permissions');
// Permissions
await knex.schema.withSchema('meraki').dropTableIfExists('permissions');
// Roles
await knex.schema.withSchema('meraki').dropTableIfExists('roles');
// Drop Extensions
await knex.raw('DROP EXTENSION IF EXISTS "uuid-ossp"');
// Delete Schema
await knex.raw('DROP SCHEMA IF EXISTS meraki');
};
答案 0 :(得分:2)
做这样的事情-
await knex.schema.withSchema('meraki').createTable('role_permissions', table => {
table
.string('role_id')
.notNullable()
.references('id')
.inTable('meraki.roles'); // scmema attached.
table
.string('permission_id')
.notNullable()
.references('id')
.inTable('meraki.permissions'); // scmema attached.
table.timestamps();
})