当前位置: 首页 > 知识库问答 >
问题:

Knex迁移的问题

孔正文
2023-03-14

我不确定似乎是什么问题,但这种膝盖迁移失败了。尽管我是编写迁移的新手,但我坚信此迁移文件是正确的。生成的错误如下

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、RolePer的、令牌)的表的代码时,其余的似乎都在工作。

'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');
};

共有1个答案

辛渝
2023-03-14

做这样的事情-

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();
  })
 类似资料:
  • 我的应用程序(Postgres)中有7个不同的模式,我做了两次迁移来更改列,一次影响A模式,另一次影响公共模式。我想知道这种意外行为的原因。

  • 我似乎无法用knex迁移数据库。在up命令中,它失败。 我得到一个错误。 我发誓它昨天还在工作。我试着删除并重建数据库。运气不好。怎么了?我该如何解决这个问题? 在knex消息源中似乎没有任何明显的挖掘。

  • 我正在使用已知来连接我的应用程序中的 postgres。我在运行时收到以下错误 参考一些线程,我知道我必须添加事务调用,但我是否需要添加应用程序的所有sql调用? 在留档中,它没有给我关于何时添加这个的详细信息?为什么是必须的?我的查询大多是“GET”类型,因此不确定这些查询是否需要应用事务?

  • 问题内容: 我刚刚在Xcode 7 Beta中打开了我的旧项目。该代码在Xcode 6中可以正常工作,但是现在显示出许多错误。我不知道这些是什么。有人可以解释为什么发生这种情况,以及如何解决它吗?谢谢!这是代码 问题答案: 这是带有Swift 2.0的do / try / catch实现的updateAction()函数:

  • 我对rails迁移有一个奇怪的问题。我的db/migrate文件夹包含迁移文件,所有文件都工作正常。但几分钟前,我使用rails g migration MigrationName创建了一个新文件,它生成了一个新文件。然后,当我运行rakedb:migrate时,它会回滚所有内容,我的模式版本变为0。现在,当我运行rake db:migrate时,它什么也不做,而db/migrate包含我的所有迁

  • 我们目前正在将Java应用程序从Oracle JDK 8(由JNLP提供的应用程序代码)迁移到OpenJDK 11(作为可运行的应用程序代码以及Java运行时提供)。尽管我们在测试环境中或多或少找到了一个工作解决方案,但我们仍然存在以下问题: > 我们的应用程序需要JavaFX,我们希望使用jlink进行构建。是否建议使用https://gluonhq.com/products/javafx上提供