简体   繁体   English

Rails将schema.rb时间戳更改为日期时间

[英]Rails changes schema.rb timestamps to datetimes

This problem seems to happen all the time when working with other developers. 与其他开发人员合作时,这个问题似乎一直在发生。 We have a table created in a migration like so (backed by postgres): 我们有一个像这样的迁移创建的表(由postgres支持):

create_table :subscription_events do |t|
  t.integer :subscriber_id
  t.text :source_url
  t.text :params
  t.text :session

  t.timestamps
end

Then at seemingly random points in time in the future after running rake db:migrate, Rails wants to update the schema.rb file to use datetime instead of timestamp , causing an additionally confusing reindentation of the whole create_table calls as well: 然后在运行rake db:migrate之后的未来看似随机的时间点,Rails想要更新schema.rb文件以使用datetime而不是timestamp ,从而导致整个create_table调用的另外令人困惑的重新注册:

   create_table "subscription_events", :force => true do |t|
-    t.integer   "subscriber_id"
-    t.text      "source_url"
-    t.text      "params"
-    t.text      "session"
-    t.timestamp "created_at",    :limit => 6, :null => false
-    t.timestamp "updated_at",    :limit => 6, :null => false
+    t.integer  "subscriber_id"
+    t.text     "source_url"
+    t.text     "params"
+    t.text     "session"
+    t.datetime "created_at",    :null => false
+    t.datetime "updated_at",    :null => false
   end

What is causing this? 是什么造成的? Should we be checking in this modified file or just reset it every time? 我们应该检查这个修改过的文件还是每次都重置它?

This should not be causing any 'reindexation', because the :datetime and :timestamp migration types are both mapped to PostgreSQL's TIMESTAMP datatype. 这不应该导致任何“重新索引”,因为:datetime:timestamp迁移类型都映射到PostgreSQL的TIMESTAMP数据类型。

This is likely caused as a result of the inherently unordered ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES constant which is defined as a standard hash. 这可能是由于固有无序的ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES常量导致的,该常量被定义为标准哈希。 When ActiveRecord searches for the first suitable match for 'TIMESTAMP', it may find either :datetime or :timestamp unpredictably (since both are matches). 当ActiveRecord搜索'TIMESTAMP'的第一个合适匹配时,它可能会发现:datetime:timestamp不可预测(因为两者都匹配)。

In short, don't fuss about it as this shouldn't affect your data or schema in the least. 简而言之,不要大惊小怪,因为这不应该影响您的数据或架构。

UPDATE UPDATE

The rails 'datatype' used in the dump is found using the simplified_type method which will return :datetime for the TIMESTAMP datatype. 转储中使用的rails'数据类型'是使用simplified_type方法找到的,该方法将返回:datetime TIMESTAMP数据类型的:datetime More likely, you have upgraded your Rails version where the previous version had a different method for determining the datatype. 更有可能的是,您已升级了Rails版本,其中先前版本具有用于确定数据类型的不同方法。 Whatever the reason, this shouldn't affect you in any way. 不管是什么原因,这不应该以任何方式影响你。

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

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