简体   繁体   English

Rails创建表迁移是否应该具有created_at和updated_at?

[英]Are Rails create table migrations supposed to have created_at and updated_at?

In my experience, Rails g migration create table migrations always added timestamps to the table. 以我的经验, Rails g migration创建表迁移总是在表中添加时间戳。 But that is not happening: 但这并没有发生:

rails g migration CreateFoo bar:references

class Foo < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.references :bar, index: true
    end
  end
end

Is this expected behavior? 这是预期的行为吗? Did this change in Rails 4? Rails 4发生了这种变化吗?

ActiveRecord 4.1.6 ActiveRecord 4.1.6

timestamps are only created with generating models. 时间戳仅通过生成模型来创建。

here's an example from the guide. 这是指南中的示例。

http://guides.rubyonrails.org/migrations.html http://guides.rubyonrails.org/migrations.html

$ bin/rails generate migration CreateProducts name:string part_number:string

generates 生成

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name
      t.string :part_number
    end
  end
end

up 向上

You can make a suggestion by looking at provided code examples there. 您可以通过查看此处提供的代码示例来提出建议。

Also look at: 还要看:

rails g migration --help
rails g model --help

and you will see that timestamps are only provided automatically with "g model" (and you can actually disable it for models) 并且您将看到时间戳仅与“ g模型”一起自动提供(并且您实际上可以为模型禁用它)

Use Rails generate model function to create model, migration and test/spec: 使用Rails生成模型功能来创建模型,迁移和测试/规格:

rails g model CreateFoo bar:references

This will add the t.timestamps in your migration. 这将在您的迁移中添加t.timestamps

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

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