开发者

db:migrate has no effect

If I edit my shema, and run db:migrate the database doesn't changed, but if 开发者_运维百科I clear to version 0, and recall migration it's works, but I lost all database data.

What's wrong?


That's how db:migrate works. It maintains a table in the database called schema_migrations that keeps track of the migration timestamps (i.e. if your file is called 20090807152224_create_widgets.rb, the 20090807152224 part is the timestamp -- and the line that will get added to your schema_migrations table).

You're not supposed to modify the schema.rb file by hand -- that file gets autogenerated as a result of db:migrate.

The thinking in rails is that if you want to make a change to your schema, you're going to generate a new migration with those changes and then run db:migrate (which, as a result, will update the schema.rb file appropriately).


When you say you are updating your schema, does that mean you're updating the db/schema.rb file, or actual migrations?

If you're updating the schema.rb file, you should note that it will have no effect because the file is auto-generated.

See the comment at the top of the file:

# This file is auto-generated from the current state of the database. Instead of editing this file, 
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.


It sounds like you were changing a migration file.

Don't change migration files. Add new ones. You can have migrations that change say, the type of a column. There are times when changing old migrations may be helpful, but don't do it unless you know the consequences. As others have mentioned, don't change the schema either, but I don't think you were doing that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜