Where do the created_at and updated_at columns come from?

混江龙づ霸主 提交于 2019-12-30 07:58:10

问题


All the tables in the database created by a rails application seem to have created_at and updated_at columns. What creates these? Are they optional, or does something internal rely on them?


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/4722785/where-do-the-created-at-and-updated-at-columns-come-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!