PrivateOmega
PrivateOmega

Reputation: 2900

Issue with Knex migration

I am not sure what seems to be the issue, but this knex migration is failing. Even though I am new to writing migrations I strongly believe this migration file is correct. The error generated is as follows

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

The code is as below. Initially those migration functions were in separate files and I assumed it was failing then because the files weren't being executed synchronously, which led me to compose a single file. I am not sure if this is anyhow helpful, but when I remove the code for tables containing foreign key references(UserRoles, RolePermissions, Tokens) rest of it seems to be working.

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

Upvotes: 1

Views: 7546

Answers (1)

Fazal Rasel
Fazal Rasel

Reputation: 4526

Do something like this-

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

Upvotes: 6

Related Questions