Programming Ecto (Pragprog)

I would like to say: What a nice front picture! Kudos :heart_eyes:!

Both Programming Phoenix ≥ 1.4 and Programming Ecto are coming on the same day, 2018-06-10. Wow!

1 Like

Awesome! Looking forward to it!

1 Like

Gone Beta!


Got the beta ebook. Excellent writing. Enjoyed reading it after using Ecto for a year and developing applications. Simply, This is the way to learn Ecto!


The b1.0 release contains:

Part - I: Ecto Fundamentals

  1. Getting Started With Repo
  2. Querying Your Database
  3. Connecting Your Tables To Wlixir Structs with Schemas
  4. Making Changes With Changesets
  5. Making Multiple Changes With Transactions and Multi
  6. Making Changes To Your Database

Part - II: Ecto Applied
7. Adding Ecto To An Elixir Application Without Phoenix
8. Working With Changesets And Phoenix Forms
9. Testing With Sandboxes
10. Creating And Using Custom Types
11. Inserting and Updating with Upserts
13. Working with Embedded Schemas

Coming soon…:
12. Optimizing Your Application Design
14. Creating Polymorphic Associations
15. Optimizing iEX For Ecto
16. Using Schemas Without Tables
17. Changing Your Database Pooling Strategy
18. Adding Streaming And High Throughput
19. Working with Database Functions

Given that covering Ecto in any real breadth and depth is really outside the scope of Programming Phoenix ≥ 1.4 it might be beneficial to cover Programming Ecto chapters 1-6 before hitting Programming Phoenix ≥ 1.4 chapter 4 Ecto and Changesets (though of course it is not strictly necessary).

1 Like

Do you mean Part I, sections 1-6?

I was going to start on Programming Phoenix next, so curious why specifically you think 1-6 of this book should be read first - does PP kind of throw you into the deep end?

Like the sound of that - would be good if the book details how to use Ecto as per PragDave’s thoughts in his course as per Discussion: Don't add a database layer to your Phoenix application :slight_smile:

Part I: pp. 3 - 116 (first six chapters)

does PP kind of throw you into the deep end?

That’s not it at all. It’s easy enough to simply follow along in PP without much trouble … however that doesn’t mean you will necessarily be left with a sense of what to do in your own application when the time comes.

Understandably PP would defer to Ecto’s documentation for further in depth study (and possibly What’s New in Ecto) after some introduction in 4. Ecto and Changesets (pp. 51-70) and 7. Ecto Queries and Constraints (pp. 118-142).

Having PE Part I under your belt (in particular 4. Making Changes With Changesets (pp. 61-84)) those chapters would be largely review and you would likely have a much better sense of what is going on.

On a personal note, I just prefer having some focused time with a particular technology. By necessity in PP you are constantly context switching between Plug & Router, Controllers & Views, EEx templates and Ecto. Having Ecto already more or less settled quiets things down a bit.

would be good if the book details how to use Ecto as per PragDave’s thoughts

That chapter, in detail, deals with the type of application setup already seen in the Getting Started Guide while elaborating on other aspects like how to set up multiple Repos. So “without Phoenix” relates more to the fact that one is doing work manually that otherwise a Phoenix generator would tend to do.


I’ve bough this book. It’s really great to improve your skills using Ecto.

1 Like

Bought it. Enjoying it so far!

1 Like


Programming Ecto (eBook) has recently been updated.
Changes in This Release

  • Added chapter 15, Optimizing iEX For Ecto
  • Renamed files containing code examples
  • Addressed errata
1 Like

Will this book be updated to Ecto 3.0 once it’s released? I want to buy it but have a few books I have to read first. Would be pitty if the book is a bit outdated once I get to it :slight_smile:


Yes, it will be updated to Ecto 3.0 once the release candidate is out.


Nice to know and by the way I totally recommend this book!! Very easy to read and follow. Thanks @ericmj @darinwilson

1 Like

This is a great book! I’m always on the lookout for new beta versions to be published so I can read the new chapters that are released.

I don’t think PragProg allows forums anymore on their site (only errata) and I have a question about the latest chapter 12. First, I really appreciate that chapter since many times books don’t get into higher level application design topics (maybe to avoid “flame wars”) :slight_smile:

Question I have is about the good “general rule” advice to put all pure functions manipulating queries, changesets, and multis into their associated schema modules and put all the impure code into context modules. Right now I put all changeset code in the schema modules but usually leave the multi (and query) code in the context module… generally since my multis and queries are affecting multiple DB tables/schemas so they don’t really belong in any specific schema module. Are you recommending maybe using Multi.merge extensively to build up the whole multi in the context module calling functions in the schema modules… or something else?

I looked at the sample code that comes with the book and can’t find much use of multi (yet). Are there any large/decent scale OSS projects I can study that follow the recommended pattern… especially as it relates to Multi or query use defined in schema modules?



Hi, glad you are liking the book.

Chapter 12 was a difficult chapter to write, balancing our experiences writing Ecto applications while still maintaining that there is no right or wrong and that there may be other equally valid ways of organizing applications. Where Multi fits best was something we discussed while writing the chapter and it seems like somewhere during the discussions some things were missed.

After discussing this again we are amending this part of the chapter and adding a paragraph that clarifies that we believe multi fits better in context modules than schema modules. The reason being, while multi is a pure data structure like changesets, with multi you usually compose functions from multiple schemas and with changesets you compose functions within a single schema. Since multi is working with multiple schemas it fits better on a higher level inside a context.

Thank you for bringing this up and helping us improve the book during the beta! :purple_heart:


You can look at hexpm the application that runs Examples of context modules using multis are: Keys, Users, and Releases.


I have started reading the book and got problem with fist exercise. I think the code provided with the book though pragrpog website is incomplete:

    08:54:23.966 [debug] QUERY OK db=2.4ms
INSERT INTO "genres" ("name","wiki_tag","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["jazz", "Jazz", {{2018, 8, 22}, {22, 54, 23, 958894}}, {{2018, 8, 22}, {22, 54, 23, 958900}}]

08:54:23.970 [debug] QUERY OK db=4.0ms
commit []

08:54:23.971 [debug] QUERY OK db=0.4ms queue=0.1ms
begin []

08:54:23.973 [debug] QUERY OK db=2.4ms
INSERT INTO "genres" ("name","wiki_tag","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["live", "Concert", {{2018, 8, 22}, {22, 54, 23, 971190}}, {{2018, 8, 22}, {22, 54, 23, 971196}}]

08:54:23.974 [debug] QUERY OK db=0.4ms
commit []
** (UndefinedFunctionError) function MusicDB.Note.__schema__/1 is undefined (module MusicDB.Note is not available)
    (ecto) lib/ecto/changeset/relation.ex:138: Ecto.Changeset.Relation.change/3
    (ecto) lib/ecto/repo/schema.ex:597: anonymous fn/4 in Ecto.Repo.Schema.surface_changes/3
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/repo/schema.ex:584: Ecto.Repo.Schema.surface_changes/3
    (ecto) lib/ecto/repo/schema.ex:189: Ecto.Repo.Schema.do_insert/4
    (ecto) lib/ecto/repo/schema.ex:125: Ecto.Repo.Schema.insert!/4
    priv/repo/seeds.exs:15: (file) 

I have check the code and function MusicDB.Note is missing.

Hi @sergek. Sorry for the trouble - I screwed up the source code when we pushed the last beta, and didn’t catch this. :disappointed:

I’ve submitted a fix, so if you download the latest version of the source from the book’s webpage, everything should be working again.

Please give that a try, and post again if you’re still having trouble. Thanks for your patience! :heart:


Thank you for the prompt fix! :grinning:

It did work much better this time. Although this time I got warnings like the ones below:
==> music_db
Compiling 14 files (.ex)
warning: invalid association notes in schema MusicDB.Artist: associated schema MusicDB.Note does not have field artist_id
lib/music_db/artist.ex:9: MusicDB.Artist (module)

warning: invalid association `notes` in schema MusicDB.Track: associated schema MusicDB.Note does not have field `track_id`
  lib/music_db/track.ex:9: MusicDB.Track (module)

warning: invalid association `notes` in schema MusicDB.Album: associated schema MusicDB.Note does not have field `album_id`
  lib/music_db/album.ex:9: MusicDB.Album (module)

Is that something to worry about?