Quick question about sequelize migrations - node.js

I'm trying to do sequelize migrations on the models I created such as User.js, Blogs.js etc... What I've noticed is that I can add columns to Models like User.js and Blogs.js, but if I dont change the files in my models folder, the changes will be recognized in the database, but I wont be able to access the new columns when the server is running.
EG:
I add column permission: {type: Sequelize.STRING} to User.js.
I can't access this column because my original model file doesn't contain permission. Do I have to delete the models, and then create them using the sequelize-cli in order to get the migrations to also update the model files?

Your question is somewhat unclear.
In order to add a new column to a table using Sequelize migrations, you need to do the following steps:
Generate a new migration file.
Using queryInterface methods, make the changes to your tables in the migration file.
Execute your migrations.
Update your models with any new properties that are added.
Restart your server.
Keep in mind that migrations are done as an ALTERNATIVE to using the sequelize.sync() method. If you are using the sync() method, you cannot add columns to a table without using the { force: true } flag inside of sync(), which causes your tables to be dropped and data lost. Migrations are a process that requires a bit more attention to be paid, but they are a feasible long-term solution for a production database.

Related

Creating new tables on the fly using sequelize

This is actually the first time I'm posting something here. The problem I have is driving me crazy since I know such functionality exists elsewhere.
I'm trying to find a way to create new tables on the fly on node js using sequelize without having to restart the database (a DB implemented with SQL).
Moreover, I also need to create new associations between those new table and existing ones.
Why do I need this:
I'm creating a crm and one of the functionalities I want to add is to let users create new types of objects that could have fields referencing to other preexisting ones.
And I can't just restart the whole database everytime a user is creating a new object or fields.
If you guys know how to achieve that or maybe advising me to build the database with another language I'll go for it.
Thanks!

sequelize force mark migration done

I re-created my database on one of my dev environment and now when I run the migration via sequelize db:migrate, it tries to run the migrations from the first.
I don't want to re-sync/re-create the database since running migrations on the dev environment ensures that the migrations are correctly written.
Is there a way to force-mark some migrations as 'done'?
Sequelize-cli stores the migration data on a table called SequelizeMeta.
You can copy the migration filename from your existing DB and insert into the above mentioned table in new environment's DB.
All the migrations recorded would be considered as they have already ran.
Though this would stop selected migrations from running, it's not the best approach to be taken.
This metadata could also be stored in a json, though I am not very aware of the structure for it.
You can dig through the docs here
The actual way of doing is as follow,
There are two tables sequelizemeta and SequelizeMeta(Hidden)
And for skip or say migration already ran is enter value in both table like
INSERT INTO sequelizemeta (name) VALUES
('20181019072815-roles.js'),
('20181019093229-users.js')
INSERT INTO SequelizeMeta (name) VALUES
('20181019072815-roles.js'),
('20181019093229-users.js')
Note- SequelizeMeta is hidden table but we can query it.
SELECT * from seerportal.SequelizeMeta

How to reflect dropped tables from models in the database using Sequelize commands?

I currently call a custom function dbinit() in terminal whenever I have updated my models and want my database to reflect those changes. This function includes the Sequelize method db.sequelize.sync(), which adds some changes to my database including new tables and columns.
Unfortunately, sync() doesn't remove tables from the database to reflect changes in my model. Is there some code I can add to my custom function that will make the database reflect tables that have been deleted?
(using PostgreSQL 9.4.4 and Node.js 5.1.0)

How to use migrations in Sequelize?

I have used ORMS such as EntityFramework, Waterline and Mongoose. I just started using Sequelize. I have created a sample model. Later I wanted to add another column to the table corresponding to the same model. When I edit the model, the new field doesn't seem to be reflecting in the table unless I drop the table. Am I supposed to use Migrations? I got confused after reading the documentation. Could someone help me out?
Yes, you should use migrations as if you edit a model in sequelize when you application starts it will only create the table if it doesn't exist. It does not check if the schema is different thus it doesn't update the schema of your table.
If I were you, I would install sequelize-cli and then run the sequelize init command. This sets up your project ready to use migrations.
Please note that we using sequelize migrations you have to explicitly define the primary key, updatedAt and createdAt columns otherwise they will not great created!
The docs are not too bad about migrations: http://sequelize.readthedocs.org/en/latest/docs/migrations/

What is the best way to add tables to Entity Framework 6 Code First to Existing Database?

I'm developing an ASP.NET MVC 4.5 project using EF 6 Code First to an Existing Database. I would like to create some new tables with foreign key relationships to one of the tables in the dbcontext I've created. I've altered and added columns in that original table, creating several migrations. There is real data in that table.
I would prefer to create the new tables in the database, but don't see how EF would generate a model for me. I can code the model myself, but don't see any documentation about how I would add it to the context class generated by EF. And then the migrations would be out of whack.
So I'm thinking that the best thing to do would be to delete all the migrations, delete the context class and drop the migrations table. Then I could start from scratch with an initial migration. Am I missing some gotcha? Is there a better way?
FWIW to others facing this dilemma, I figured it out. First I got rid of all the migrations, following the 100+ up-voted answer here: Reset Entity-Framework Migrations
Second, I created new the tables and constraints I needed in the database.
Third, I created a new entity in my solution and generated model classes from the database. I changed calls from the old entity to the new entity.The generator overwrote the model for the original table, but since I have all the annotations in version control, it is trivial to paste them in.
If I need to, I can enable migrations again.
Hope this helps.

Resources