Where do the created_at and updated_at columns come from?

21,356

Solution 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.

Solution 2

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

Solution 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.

Share:
21,356
kdt
Author by

kdt

Updated on July 09, 2022

Comments

  • kdt
    kdt almost 2 years

    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?

  • rajansoft1
    rajansoft1 about 10 years
    is their any way we can rename it to say created_ts may be?
  • Meekohi
    Meekohi almost 5 years
    @rajansoft1 there is not, the names are hardcoded -- although you can use created_on/updated_on instead of created_at/updated_at if you prefer.