rails无缘无故地更改schema.rb

wes*_*ool 8 ruby-on-rails rails-migrations

每次我运行rake db:migraterails都决定更改我的schema.rb文件.在某些情况下,这是完全合理的,但在其他一些情况下,它似乎无缘无故地这样做.我很困惑的情况是当我从git中提取一个新的migration.rb和一个新版本的schema.rb,然后运行rake db:migrate.由于schema.rb文件的新版本附带此迁移,因此我不应该更新schema.rb.但是,rails每次都会改变它.当发生这种情况时,我发现了令人难以置信的愚蠢变化

add_index "my_table", ["column1", "column2"], :name => "index_on_some_columns"
Run Code Online (Sandbox Code Playgroud)

add_index "my_table", ["column2", "column1"], :name => "index_on_some_columns"
Run Code Online (Sandbox Code Playgroud)

当这种情况发生时,我只是继续奔跑git checkout db/schema.rb,继续我的生活,但它让我永无止境.是否有这样做的原因,我怎么能阻止它这样做?

编辑:这是一个差异的摘录

@@ -165,12 +165,11 @@ ActiveRecord::Schema.define(:version => 20130206001907) do
     t.column "updated_at", :datetime
-    t.column "coordinates", :point, :srid => 4326
@@ -200,15 +199,16 @@ ActiveRecord::Schema.define(:version => 20130206001907) do
     t.column "something", :boolean
+    t.column "coordinates", :point, :srid => 4326
+    t.column "random_string", :string
     t.column "remote", :string
-    t.column "random_string", :string
   end

-  add_index "my_table", ["id", "foreign_id"], :name => "index_active_my_table_on_foreign_and_id"
-  add_index "my_table", ["id", "active"], :name => "index_my_table_on_active_and_id"
-  add_index "my_table", ["id", "content_modified_at"], :name => "index_my_table_on_content_modified_at_and_id"
+  add_index "my_table", ["foreign_id", "id"], :name => "index_active_my_table_on_foreign_and_id"
+  add_index "my_table", ["active", "id"], :name => "index_my_table_on_active_and_id"
+  add_index "my_table", ["content_modified_at", "id"], :name => "index_my_table_on_content_modified_at_and_id"
Run Code Online (Sandbox Code Playgroud)

ben*_*ado 6

由于schema.rb文件的新版本附带此迁移,因此我不应该更新schema.rb.

这是不准确的.

每次Rails运行迁移时,它都会schema.rb使用数据库作为源来更新文件.它不会查看现有schema.rb文件,只是使用数据库中的信息并覆盖它.

看来真正的问题是,在生成schema.rb文件时,在两个不同的环境(Ruby,Rails,MySQL,操作系统的不同组合)中运行相同的迁移可能会产生不同的结果.

解决方案是确保每个签入代码的人都在尽可能使用相同的软件版本.如果不可能(因为这是Windows与Linux与Mac的区别,并且您不想改变您的操作系统),您只需要处理不便之处.