Is this possible or must the entire project be nuked and redone?
Example: I want to add a "breedId" column to a dogs table to reference model "Breeds"
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Dog', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING(50),
allowNull: false
},
description: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Dog');
}
};
Since you appear to be using migrations, you will have a pretty easy time adding onto your tables.
Create another migration, and use queryInterface.addColumn(tableName: string, columnName: string, options: object) for the up, and queryInterface.removeColumn(tableName: string, columnName: string) for the down.
If you are using sequeilze.sync() to build your tables from the models, your options are worse. You either need to pass {force: true} into the .sync() method, which will drop the current tables before rebuilding them with the attributes in your models. Or, to preserve data you can omit {force: true} by manually adding the columns and foreign key constraints to the database, and add the corresponding attributes to the models.