简体   繁体   中英

Ruby on Rails : purpose of db:migrate

When I read Rails book, each time they create a new database, always follow a db:migrate .

rails generate scaffold school
rake db:migrate

In console view, I see at first line, Rails create some files, no problem. but in second line, I see that Rails isn't really change anything. I have view some files that Rails nearly create and see no change too.

So, what the purpose of line 2, please tell me.

Thanks :)

The rake migrates the changes into your database. It is which acttually changes the database schema to match your previously generated scaffolded model.

Without it, you wouldn't have a table to write your objects into. Or in case of changed model, the table could differ from your model, leading to error.

When you generate a model (or scaffold one) a migration file is created in your db/migration directory. It is a pure text file, you can create such manually, if you want. This is the tool for the iterative development in rails regarding the database. Each migration adds some change to the system. When you run rake db:migrate your database is updated by the given migrations. This is a handy tool in case of distributed development, when one programmer can check out the code from the repository, and can run the migrations on his own development database.

db:migrate , is the command that tells rails to update the database with new changes. Think of it as this way

when u say rails generate scaffold rails will generate files like a model, controller etc.. and it create a file under db/migrate which has the sql script to update the database.

Ex: if you run rails generate scaffold User name:string , then you will need a table called users in the database with the column 'name', that sql script will generated under db/migrate folder

with db:migrate , command, you are telling rails to migrate new sql scripts to the database, in the above case, it will creates the 'users' table

if you run rake -T , from your rails application root, you could see all the rake tasks

HTH :)

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