Errol Hassall

View Original

Creating migrations in Ecto

Ecto has a neat way to create database tables, there called migrations. Migrations simply give the database adapter a schema to follow. The adapter is the little bit of code that turns the Ecto code into the specific database SQL. For the typical Elixir setup this would be Postgres, using the Postgrex adapter.

Migrations are such an easy way to get up and running, you write a few lines and you’ve got your columns, you add `reference(:table_name)` and you’ve got your foreign key to another table. It’s really that simple! I’ll run you through an example I was working on today.

 

First we run the command ecto generation command like so:

I’ve included two separate lines to give you an idea of what changes in this command. We use the `mix ecto.gen.migration` command with a table name after, so in our example the table names are `code_examples_table` and `language_table` Now do note that these aren’t the table names, these names are just the name that gets attached to the file. What gen.migration does, is it creates a file with the current timestamp, followed by `create_code_examples_table` so it tacks on what you named the file to the timestamp and gives you this:

These timestamps give elixir a way to organise what should be run first, when you create the database. In this scenario it will run from top to bottom. This is important to understand because it will save you a lot of time when you can’t figure out why some tables get created then you get an error because it says you haven’t created the tables required for a foreign key. Say for example if you created a `post` table, this has an `author`, thus it has a foreign key to an `author` table. Now if you ran `ecto.gen.migration` on the `post` table first, then thought “o right, posts need an author” so you go and create the `author` table, you put in the foreign key to the `post` table, create your database, you run in to an issue, because Ecto will try and create the `post` table first, see that it has a foreign key, but the foreign key points to a table that doesn’t exist. You simply edit the timestamp and off you go, it will work fine.

 

After you have generated the file, you might actually like to put some code into it. Here's some of mine:

You put everything into this `change` function. When we create a table we have to use the `create` macro, with the table function, you give it a table name. Add some columns like so, then finally add the timestamps, which is a great addition, it simply adds the two columns: “inserted_at” and “updated_at” which can be very handy.

What you might have noticed is that I used the `references` function this allows you to link the table to another as a foreign key. In this example it links the `code_example` to the `programming_languages`.

 

To actually run the migration to get it to populate the database you need to use the following command:

 

If you have some data that you need to input from your seed file, which is just a file that you can use to populate a database from scratch which is really helpful when you need to test a complex system, you can easily populate thousands of records in seconds, in any way you like.

This command will drop the database, recreate it and then run the seed file, filling it with data.

If you already have a database with data that you don’t want to lose, you will have to run this command in stead, which will change the tables but leave the data in there.

Here is the finished product, I added a third table because this is being used for a project I’m working on, so don’t worry it doesn’t change anything. I put some dummy data in and as you can see, all the columns are there and both the foreign keys have been populated.

As you can see the foreign keys link to their respective tables.

I’ve just shown you that in 5 minutes or so you can have a database up and running. In a future blog, I will discuss how easy it is to set up a seed file which allows you to input thousands of records in seconds, this makes it really simple and quick to get a database up and running.


I’m always learning something new and with Pluralsight I can have unlimited access to all things programming. But hold on, it’s not just programming. There’s photoshop, visual communication, manufacturing and design. There's a whole bunch! Sign up today and get a free 10-day trial!