ERROR: Failed to open the referenced table 'rol'

Open
#1,195 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
javascript, mysql
Domain
database

Research direction

Compare the table names and foreign-key reference in create-rols.js and create-users.js, then check the migration order. Rerun the migrations against MySQL and confirm that users is created with a foreign key referencing an existing table and that the migration completes without the referenced-table error.

Written by the indexing model from the issue text.

Description

What you are doing?

Hello everyone,

I am having problems with creating the tables in my DB, the relationship is one to one between rols and users, but it gives me failure to take the foreign key in the users table, the code of the migrations is the following:

create-rols.js

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('rols', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      nombre: {
        type: Sequelize.STRING
      },
      code: {
        type: Sequelize.STRING
      },
      status: {
        type: Sequelize.BOOLEAN,
        defaultValue:true,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('rols');
  }
};

create-users.js

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      email: {
        type: Sequelize.STRING
      },
      nif: {
        type: Sequelize.STRING
      },
      password: {
        type: Sequelize.STRING
      },
      status: {
        type: Sequelize.BOOLEAN,
        defaultValue:true
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      rol_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references:{
          model:'rol',
          key:'id'
        }
      },
    }).then(() => queryInterface.addIndex('users', ['rol_id']));
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('users');
  }
};

What do you expect to happen?

Create a foreign key field in my users table

What is actually happening?

ERROR: Failed to open the referenced table 'rol'

Dialect: mysql
Database version: lastVersion
Sequelize CLI version: 6.5.2
Sequelize version: 6.25.5

Dominant language
JavaScript
Stars
2.6k
Forks
524
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sequelize/cli

All issues in sequelize/cli

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.