开发者

Where do the created_at and updated_at columns come from?

All the tables in the database created by a rails application seem to have created_at and updated开发者_C百科_at columns. What creates these? Are they optional, or does something internal rely on them?


They are created by default when you run the ActiveRecord migration for a model. ActiveRecord automatically populates/updates them when you create or update a model instance (and thus the underlying database table row) respectively.

You can remove the columns by removing the t.timestamps line from within the model migration file.

  • Ruby on Rails Guides: Migrations


In your database migration for every table you have something like t.timestamps. Remove this from your migration and your database columns created_at and updated_at won't be created.

Edit:

In case you need to create a new migration to remove those columns you can use remove_timestamps or remove_column

remove_timestamps definition shows how you can use remove_column if you want to.

def remove_timestamps(table_name, **options)
  remove_column table_name, :updated_at
  remove_column table_name, :created_at
end


Adding to what Octopus said, they are optional and are used to track the record creation and updating date time in the corresponding tables.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜