This video is free to watch - hope you enjoy it! If you like what you see, there's a ton more where that came from...

Buy Now

Working With Ecto and PostgreSQL

You have to store data somewhere and Elixir (and Erlang in general) give you quite a few tools to do so. In this video we'll see how to push data into PostgreSQL using Ecto, the ORM created by the Elixir team.

This is premium content - you can own it in just 30 seconds!

Buy Now

An ORM? In a functional language? Wait a minute…

Yes, it’s an argument you’ll likely sit through a few times in the Elixir community. I certainly have. I have a strong opinion on this (which will become obvious in a second) but overall here’s the thing:

  • you need to store data
  • we’re using a relational database to do so, which Ecto (the data tool created by the Elixir team) supports
  • that data is structured in some way

The Rub

Here comes the opinion… ORMs in the object-oriented world are plagued by issues relating to object-oriented programming. All of that centers around the idea that a relational database, as a concept, does not share the same general fundamental principles and concepts that object-oriented programming does. This is the “impedance mismatch” you’ll hear about if you dive into the subject long enough.

It comes down to this: _there are things you can do with for loops, conditional branching, polymorphism, inheritance and is-a / has-a that are simply not part of the database vernacular. The whole idea of “lazy vs. eager loading” is part of this (how deep do you go to load up a related record, for instance).

My opinion is that functional programming is no immune to this problem of impedance mismatch. You’ll hear Elixirists proclaim loudly that “there are no objects in Elixir!” while happily pumping data into structs with defaults set and a structure implied. Immutability does not mean something is not an object. This leads me to the final point…

One of the most difficult problems with using a relational database as a data store is that you have to map the data from the store to the structure (let’s not kid ourselves it’s an object). You have to resolve the database types (varchar, text, bigint, date, date with a timezone) to whatever the corresponding type is in the language. This is the next problem to solve.

How do you map a PostgreSQL JSONB field to a field on a struct? That’s right: you map it somehow.

So: that’s the issue we have to deal with. Ecto is an ORM that doesn’t think it’s an ORM and the team steadfastly refuses to believe they are subject to the same issues as an ORM.

I could be wrong, they could be right. I’ve written 3 ORMs in my career and I’ll never do it again. I have scars to prove it. I feel each of those scars flare up when I use Ecto…

OK - opinion aside… let’s get to it…

Data Time!

You’ve got the basics down, I think. You might have to do some Googling but the time to make our move has come.

It turns out that the CTO, Sara, has tasked some developers to build a Physics library using .NET! We have a meeting later today to discuss the future of the Science Program with the board and they want to see our work!

I need this space station operational!

Let’s get to work.

Understanding Persistence and OTP

Let’s take a look at our SolarFlareRecorder using an Agent one more time:

defmodule SolarFlareRecorder do
  use GenServer

  def start_link do #example 1
    Agent.start_link fn -> [] end

  #public API
  def record_flare(pid, {:flare, classification: c, scale: s} = flare) do
    Agent.get_and_update pid, fn ->
      new_state = List.insert_at flares, -1, flare
      {:ok, new_state}

  def get_flares(pid) do
    Agent.get pid, fn(flares) ->

In the last chapter I mentioned I would be coming back to the idea of persistence with our Agent. Now that you know a little bit about OTP and how Agents work - how would you handle data persistence with our SolarFlareRecorder? Let’s think about this for a second before we dive in.


The Code

Code for this video (and for the entire series) can be found in the production GitHub repo.

  • Welcome to Red:4

    Welcome to Red:4 - Where the Future Starts Yesterday. Blah blah blah we hope you like it here blah blah… We don't have a lot of time so let's just get to it shall we?

  • Letting Elixir Soak In

    Elixir syntax can be familiar and weird at the same time, especially if you're coming from Ruby. To prepare for this section, let's clear our minds - let go of familiar syntax cues and 'the way it's done' in the languages you already know.

  • Functional Programming Primer

    Functional programming has been around forever; it's nothing new. Elixir is a functional language, as is its progenitor, Erlang. If you're coming from an Object-oriented language like Java, Ruby, C#, Python or JavaScript, functional programming will take some getting used to.

  • I need you to setup a project

    You can think of Mix as your Elixir Utility Belt. It builds your projects, runs tasks for you, runs your tests, installs packages - a fascinating tool. That comes later - right now open up your terminal and navigate to a directory where you can save your work.

  • Calculating Escape Velocity

    I hope you're prepared to learn on the job. I'm about to ramp things up on you and it's for the simple reason that I'm under the gun - and to be honest *so are you*. We just got a new CTO, she arrived a few weeks after I got the job. She doesn't like me... which I guess isn't all that surprising.

  • Pattern Matching Basics

    Pattern Matching in Elixir is one of those things that can be vexxing, interesting, or obvious. Ultimately, however, it will be confusing if you have never dealt with it before.

  • Calculating Orbital Distance

    In this part we tidy things up, plugging in Authentication and hooking our download service into Firebase Storage.Nice work so far! The Science Team loves what we're doing and, as you might expect, have given us more work.

  • Debugging

    I'm always amazed when I manage to write Elixir code that compiles correctly the first time. It almost never happens! Let's have a look at a normal sequence you might stumble through, and I'll see if I can save you some time.

  • Solar Flare Project Setup

    We need to get rolling which means we need to setup a project and start cranking out some code! In this video we'll structure our project with tests and get to know the difference between Elixir script files and compilable executables.

  • List and Enumeration Basics

    Functional programming is all about transforming data and if we're going to work with Solar Flares, we need to understand how Lists work - how we query, slice, dice, add and remove. That's what we're going to do now.

  • Recursion and Refactoring Our Solar Flares

    Most developers know about recursion and have used it once or twice in their careers; perhaps more. It can be quite difficult to get right - this is not the case with Elixir. With a combination of pattern matching and the head/tail functionality of Lists, recursively iterating over lists is straightforward.

  • if, unless, and cond

    Here's something weird to think about: in a high-level functional language such as Elixir, writing conditional code is not exactly idiomatic. Using a combination of pattern matching and syntax rules we can write extremely clean code free of if statements and conditionals in general

  • A Quick Look at OTP

    You've seen a lot of Elixir so far and have learned some new concepts, especially if you're new to functional programming. No doubt that along the way you've probably been wondering, however: *how is this all supposed to work together? how can I actually build something?*. I've showed you a few little hints, but it's time to get explicit on this

  • Working With Ecto and PostgreSQL

    You have to store data somewhere and Elixir (and Erlang in general) give you quite a few tools to do so. In this video we'll see how to push data into PostgreSQL using Ecto, the ORM created by the Elixir team.

  • Troubleshooting OTP Errors

    We're in the weeds now! Sorry for the pain, but hey, backing over a cliff is what we do as developers isn't it? You *will find yourself completely stuck* when working with Elixir and I need you to be able to reason your way out of this mess.

  • Bombs Away!

    Developers often confuse *asynchronous* execution with *concurrent* or parallel execution. I do this often! It's something I learn and unlearn, all the time. Let's take a small tangent and discuss the difference.

Watch Again



Next Up