Why timestamp in migration and schema

Hi all

Why do I have to put timestamp method in the schema and migration, why not only in the schema?


Well, the miration creates the table in the database and the scheme does tell Ecto how the table looks like.

So you have to tell the miration what you want to have and you have to tell the schema what is actually there.

Just rememer, you can create your complete database without a single migration by using a SQL script or similar.


Migrations must be run in a certain order.


Migration 01 “creates user table”.

Migration 02: “adds email field to user table”.

You can’t run Migration 02 before Migration 01, right?

You could just assign a numbering sequencing to the Migrations in order to run them in a queue.

That’s actually how it was done in Rails’ early days…

But when working with code distributed among many developers that proved to be an issue.

So the solution is to timestamp the migrations in order to know in which sequence to run them.


This question was not about the naming of the migration scripts, but about the usage of the timestamp macros inside a schema or migration.


Thanks guys.

1 Like