简体   繁体   English

rails无缘无故地更改schema.rb

[英]rails changes schema.rb for no reason

Everytime I run a rake db:migrate rails decides to change my schema.rb file. 每次我运行rake db:migrate rails决定更改我的schema.rb文件。 In some cases this is completely reasonable, however in some other cases it seems that it's doing it for no reason. 在某些情况下,这是完全合理的,但在其他一些情况下,它似乎无缘无故地这样做。 The case I'm confused about is when I pull a new migration and a new version of schema.rb from git, and then run rake db:migrate . 我很困惑的情况是当我从git中提取一个新的migration.rb和一个新版本的schema.rb,然后运行rake db:migrate Since the new version of the schema.rb file came with this migration, I shouldn't be updating schema.rb. 由于schema.rb文件的新版本附带此迁移,因此我不应该更新schema.rb。 However, rails still changes it, every time. 但是,rails每次都会改变它。 When this occurs I find incredibly silly changes such as: 当发生这种情况时,我发现了非常愚蠢的变化,例如:

add_index "my_table", ["column1", "column2"], :name => "index_on_some_columns"

to

add_index "my_table", ["column2", "column1"], :name => "index_on_some_columns"

When this happens I simply run git checkout db/schema.rb and go on with my life, but it irkes me to no end. 当发生这种情况时,我只需运行git checkout db/schema.rb并继续我的生活,但它让我永无止境。 Is there a reason why it does this, and how can I stop it from doing this? 是否有这样做的原因,我怎么能阻止它这样做?

EDIT: Here's an excerpt from a diff 编辑:这是一个差异的摘录

@@ -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"

Since the new version of the schema.rb file came with this migration, I shouldn't be updating schema.rb. 由于schema.rb文件的新版本附带此迁移,因此我不应该更新schema.rb。

This is inaccurate. 这是不准确的。

Every time Rails runs a migration, it updates the schema.rb file using the database as a source. 每次Rails运行迁移时,它都会使用数据库作为源更新schema.rb文件。 It doesn't look at the existing schema.rb file, it just uses information from the database and overwrites it. 它不会查看现有的schema.rb文件,它只使用数据库中的信息并覆盖它。

It appears that the real issue is that running the same migration in two different environments (different combinations of Ruby, Rails, MySQL, operating systems) may yield different results when generating a schema.rb file. 看来真正的问题是,在生成schema.rb文件时,在两个不同的环境(Ruby,Rails,MySQL,操作系统的不同组合)中运行相同的迁移可能会产生不同的结果。

The solution is to make sure everybody checking in code is using the same software versions, to whatever extent possible. 解决方案是确保每个签入代码的人都在尽可能使用相同的软件版本。 And if it isn't possible (because this is a Windows vs. Linux vs. Mac difference and you don't feel like changing your OS) you'll just have to deal with the inconvenience. 如果不可能(因为这是Windows与Linux与Mac的区别,并且您不想改变您的操作系统),您只需要处理不便之处。

For me the solution was making a rake db:schema:load first. 对我来说,解决方案是制作一个rake db:schema:loadrake db:schema:load And than rake db:migrate stopped changing my schema.rb for no reason. 并且比rake db:migrate停止更改我的schema.rb无缘无故。

CAUTION: rake db:schema:load will delete ALL of your existing data and recreate the database in respect of existing schema.rb. 小心: rake db:schema:load将删除所有现有数据,并根据现有schema.rb重新创建数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM