Reputation: 3644
I'm working through an Author hasMany Books example and am attempting to run a sequelize-cli migration, but am getting the following issue when I run the following migration:
ERROR: relation "authors" does not exist
This is the first migration to create an author:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Authors', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
dateOfBirth: {
type: Sequelize.DATEONLY
},
dateOfDeath: {
type: Sequelize.DATEONLY
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Authors');
}
};
The second migration to create a book:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Books', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING
},
summary: {
type: Sequelize.STRING
},
isbn: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Books');
}
};
The migration to create the relationship between Author and Book:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn(
'Books', // name of source model
'AuthorId',
{
type: Sequelize.INTEGER,
references: {
model: 'authors',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
}
)
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn(
'Books',
'AuthorId'
)
}
};
And these are my models:
author.js:
'use strict';
module.exports = (sequelize, DataTypes) => {
var Author = sequelize.define('Author', {
firstName: { type: DataTypes.STRING, allowNull: false, len: [2, 100] },
lastName: { type: DataTypes.STRING, allowNull: false },
dateOfBirth: { type: DataTypes.DATEONLY },
dateOfDeath: { type: DataTypes.DATEONLY }
}, {});
Author.associate = function (models) {
// associations can be defined here
Author.hasMany(models.Book);
};
return Author;
};
book.js:
'use strict';
module.exports = (sequelize, DataTypes) => {
var Book = sequelize.define('Book', {
title: { type: DataTypes.STRING, allowNull: false, len: [2, 100], trim: true },
summary: { type: DataTypes.STRING, allowNull: false },
isbn: { type: DataTypes.STRING, allowNull: false }
}, {});
Book.associate = function (models) {
// associations can be defined here
Book.belongsTo(models.Author);
};
return Book;
};
I've tried all sorts of things to no avail. My guess would be that it is attempting to alter the table in an asynchronous manner, but the previous migrations ran and finished:
I'm using the following:
"pg": "^7.4.3"
"sequelize": "^4.37.10"
"sequelize-cli": "^4.0.0"
"express": "~4.16.0"
I'm very new to sequelize and any help would be appreciated!
Upvotes: 7
Views: 12121
Reputation: 299
I had a similar issue and I resolved it following the "Model Synchronization" explained here.
In the documentation explains that with MODELNAME.sync() sequelize checks if the table exists and if not exists this function create the table. This function has some options:
User.sync()
- This creates the table if it doesn't exist (and does nothing if it already exists)
User.sync({ force: true })
- This creates the table, dropping it first if it already existed
User.sync({ alter: true })
- This checks what is the current state of the table in the database (which columns it has, what are their data types, etc), and then performs the necessary changes in the table to make it match the model.
You can use sequelize.sync()
to automatically synchronize all models.
Upvotes: 0
Reputation: 1150
I was facing similar issue, so how I got it work is -
Copying the code which was there in that migration file and deleting the migration file.
Then I generated a new migration file and pasted the code which I copied earlier and ran sequelize db:migrate
and it worked.
Upvotes: -1
Reputation: 7292
You have created Authors
table but referencing it with a small a
. It should be like
references: {
model: 'Authors',
key: 'id'
},
Upvotes: 13