简体   繁体   中英

Nodejs / PostgreSQL sequelize - How to update /change a model in the database?

I created a model using sequelize (the code is below) but dont understand how I can update the model. When I changed the model, the old one (with name,num_stars and amenities) still appears in my db and I thought as long i use - sync() - I do update model changes, but apparently not. Can anyone help? Thank you!

my postgreSQL model: db/index.js:

var db = new Sequelize('postgres://localhost:5432/ajax', {
 logging: false
});

var Hotel = db.define('hotel', {

    name: Sequelize.STRING,
    num_stars: {
     type: Sequelize.INTEGER,
     validate: { min: 1, max: 5 }
   },
    amenities: Sequelize.STRING
})

module.exports=Hotel;

part of server.js

app.listen(1337, function () {
    db.sync()
    .then(function(){
        console.log('Server listening!');
    });
});

In Sequelize.js, updating a model falls under the migration progress and the only way I know how to do it is via the Sequelize CLI (npm install -g seqelize-cli). This previous post should give you all the answers you need!

Just figured out that all I had to do was to add - sync: true -

var db = new Sequelize('postgres://localhost:5432/ajax', {
logging: false,
sync: true 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM