3

I made a table using sequelize-cli, however I forgot to add a column: title.

So I generated new migration:

$ sequelize migration:create --name update-notes 

And put these codes inside of migration file:

'use strict'; module.exports = { up: (queryInterface, Sequelize) => { return queryInterface.addColumn( 'Notes', 'title', Sequelize.STRING ); }, down: (queryInterface, Sequelize) => { return queryInterface.removeColumn( 'Notes', 'title' ); } }; 

After run migration and check the table schema from DB, it works:

$ sequelize db:migrate 

However model doesn't have my new added column yet:

'use strict'; // models/notes.js module.exports = (sequelize, DataTypes) => { const Notes = sequelize.define('Notes', { content: DataTypes.TEXT // NO 'TITLE' COLUMN }, {}); Notes.associate = function(models) { // associations can be defined here }; return Notes; }; 

What am I missing? How should I apply my updates to model file?

Also what if previous data in table is not compatible with new table schema? Is it just fail the migration and developer fix the issue manually?

3
  • 1
    With Sequelize migrations, you will need to manually update both your model and migration files. Running a new migration will not automatically update your model file. Commented Oct 21, 2018 at 19:35
  • @mcranston18 Oh, that's bad news 😢😢 However thanks, dude! Commented Oct 22, 2018 at 3:00
  • 1
    @mcranston18 Hey, would you post to answer to I can accept it? Commented Oct 25, 2018 at 5:37

1 Answer 1

9

Unfortunately, with Sequelize migrations, you will need to manually update both your model and migration files. Creating a new migration file will not update your model file, and vice versa. Though the Sequelize CLI offers a few helper options, you're generally on your own to ensure your model file and migration file are defined correctly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.