admin管理员组

文章数量:1221441

I will get through to the point already. I'm having a problem of updating the rows after I have changed the status column attribute.

up: function(queryInterface, Sequelize) {
    return queryInterface.changeColumn('projects', 'status', {
        type: Sequelize.ENUM('processing', 'unassigned', 'ongoing', 'completed'),
        allowNull: false,
        defaultValue: 'unassigned'
    }).then(function() {
        return Project.update({
            status: 'unassigned'
        }, {
            where: {
                status: 'processing'
            }
        });
    });
}

The Project.update() seems not working in any case but changing the attributes of the column works.

Any idea guys? I'm somehow a newbie in sequelize and any idea would be a great help. Thanks.

I will get through to the point already. I'm having a problem of updating the rows after I have changed the status column attribute.

up: function(queryInterface, Sequelize) {
    return queryInterface.changeColumn('projects', 'status', {
        type: Sequelize.ENUM('processing', 'unassigned', 'ongoing', 'completed'),
        allowNull: false,
        defaultValue: 'unassigned'
    }).then(function() {
        return Project.update({
            status: 'unassigned'
        }, {
            where: {
                status: 'processing'
            }
        });
    });
}

The Project.update() seems not working in any case but changing the attributes of the column works.

Any idea guys? I'm somehow a newbie in sequelize and any idea would be a great help. Thanks.

Share Improve this question asked Jul 30, 2016 at 7:26 JayRJayR 4411 gold badge4 silver badges16 bronze badges 1
  • Related: stackoverflow.com/questions/18742962/… The feature request: github.com/sequelize/cli/issues/862 – Ciro Santilli OurBigBook.com Commented Aug 6, 2022 at 20:14
Add a comment  | 

1 Answer 1

Reset to default 16

Depending on how you execute the migration ( via sequelize-cli or programmatically via umzug ). There is a different way to expose the table via the ORM.

In your case you have queryInterface passed as an argument to your function. So you can do a "raw query" via the attached sequelize property.

up: function(queryInterface, Sequelize) {
    return queryInterface.changeColumn('projects', 'status', {
        type: Sequelize.ENUM('processing', 'unassigned', 'ongoing', 'completed'),
        allowNull: false,
        defaultValue: 'unassigned'
    }).then(function() {
        return queryInterface.sequelize
                             .query("UPDATE projects SET status='unassigned' WHERE status='processing'");
    });
}

By doing this you will make a raw Query to your database.

You can check out this gist for more details on an advanced way of using the ORM inside the migration.

I'm a fan of using umzug programmatically, which executes the migrations and also provides the initialized models of your database. If you configure it properly, you will benefit the exposed models ( e.g. sequelize.model('project').update() ) and have a better looking code.

本文标签: javascriptSequelize Migration update model after updating column attributesStack Overflow