ERROR: Failed to open the referenced table 'rol'
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sequelize/cli
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·