Sequelize: Changing model schema on production

ε祈祈猫儿з 提交于 2019-12-04 16:44:26

问题


We're using the orm sequelize.js and have defined a model as such:

module.exports = function(sequelize, DataTypes) {
    var Source = sequelize.define('Source', {
        name: {
            type: DataTypes.STRING, 
            allowNull: false, 
            unique: true
        }
    }, {
        paranoid: true
    });

    return Source;
};

This is deployed to production and sync'd to the database using sequelize.sync. Next step, we add a parameter:

module.exports = function(sequelize, DataTypes) {
    var Source = sequelize.define('Source', {
        name: {
            type: DataTypes.STRING, 
            allowNull: false, 
            unique: true
        }, 
            location: {
                    type: DataTypes.STRING
            }
    }, {
        paranoid: true
    });

    return Source;
};

However, when deploying to production sequelize.sync does not add this new parameter. This is because sync does a:

CREATE TABLE IF NOT EXISTS

And does not actually update the schema if the table exists. This is noted in their documentation.

The only option seems to be to { force: true }, however this is not okay for a production database.

Does anyone know how to properly update the schema when changes are necessary?


回答1:


You want to implement Sequelize migrations:

http://docs.sequelizejs.com/manual/tutorial/migrations.html

These will enable you to transition developer, staging, and production databases between known states.




回答2:


A quicker way would be using {alter: true} option.

Ref: https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-method-sync



来源:https://stackoverflow.com/questions/17708620/sequelize-changing-model-schema-on-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!