Rudrika Fichadiya
Rudrika Fichadiya

Reputation: 13

How to Add column after, when table already have data using Sequelize in NodeJS?

I have models defined in the project, where I need column insertion is needed, for that I have just added the new column and its type to the particular model file.

Also tried with {alter:true} but not reflected in Database table.Also having error while inserting a new record, Unknown column 'isActive' in 'field list'

Upvotes: 0

Views: 1476

Answers (2)

sharad pratap singh
sharad pratap singh

Reputation: 1

You can try this below after importing that file

await sequelize.sync({ alter: true })
    .then(() => console.log('Database schema updated'))
    .catch((error) => console.error('Error syncing database:', error));

Upvotes: 0

Mansur Ali Koroglu
Mansur Ali Koroglu

Reputation: 1918

The error is because of your database is not updated with the new model, so it's normal. After updating your database successfully it won't be a problem.

It's hard to say anything before looking at the code but I guess your import (require) logic is the problem. Even if you fix that you are using sync function which is not good for production. It's just for testing purposes.

You should use sequelize migrations. With the help of it you can track your db changes and do not lose important data. It is a must in production but it's still good to use in development.

Read Sequelize Migrations

Upvotes: 0

Related Questions