How to use migrations in Sequelize? - node.js

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/

Related

Quick question about sequelize migrations

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.

Table does not exist Sequelize

I created a customer model, then this is the model
then i tried to insert data into it sequelize command
after i sent the post request to add the data here is what I got sequelize error
the name of the model and table is customer (singular) but sequelize is looking for customers (plural).
Ok, the problem is if you want singular table name other than auto generated plural name, you should update Sequelize configuration.
You can checkout this question and its answer.

How to use ORM while table already define in database?

I'm using "sequelize" and "sqlite3"
But I don't know how to do even if the table s already exist in database.
i have seen the document , but it seems that must define the schema everytime even if the table already exist ?
If the table already in database , how can I use it without define schema again?
You can use sequelize-auto npm module for the same.
https://github.com/sequelize/sequelize-auto
I had the same situation before and used this npm module to generate the models, later can tweak or do some minor change if needed.
Schema (model) definition only represents table in database and does not create table itself.
If table already exist you need to define schema for this table only when app starts.
FWIW, if the table already exists, you need not use sync(); you can begin with User.create().

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

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