Programming Ecto (Pragprog)

2018-09-05

3 Likes

Does anyone know whether this book will be updated to reflect the changes in Ecto 3.0?

Thanks in advance!

Posted on July 1st above :slight_smile: .

4 Likes

2018-11-02

1 Like

2019-01-24

This is just to let you know that Programming Ecto (eBook) has recently been updated. You own an electronic version of this book, and so you’ll be able to download this latest version.

Changes in This Release

  • Added chapter 17, Tuning For Performance
  • Made several edits and additions throughout to book to reflect the changes for Ecto 3.0
  • Fixed many errata
4 Likes

Aaaaand that’s a wrap - the book is officially completed and released!

Thanks to all who participated in the beta - the early feedback was super helpful :heart:

https://pragprog.com/book/wmecto/programming-ecto

13 Likes

Interim-review (continued in post below)

It’s summer here and as I try not being glued to my desk all day, I needed a book to read where it wasn’t strictly necessary to follow along with the examples. I chose this book after what @peerreynders said in his post above :smiley:

I’m only 60% of the way through this book (chapters 1-6 as per Peer’s recommendation, and will be going back to Programming Phoenix now - edit - I actually ended up finishing the book as I was enjoying it so much! See the rest of my review in the post below…) but I just wanted to post a mini-review to say I’m really really really loving this book! It’s been one of the most enjoyable Elixir books I’ve read so far. Why? Because it’s sooooooooooooooooooooooooooooo easy to read!! :lol:

There are a number of reasons for this, in part it is because of the pace and well thought out progression of topics, but the thing I really appreciated, is that it seems to have been produced with those of us who use e-readers in mind; there’s absolutely no having to flick back through pages to review code that is spoken about. Code is either just before or just after it’s covered or (and I really appreciate this…) repeated if it is spoken about again. The output is also included, eradicating the need to actually follow along as the results are there in front of you. On top of that, since everything is well explained and at an even pace, the cognitive load required to progress through the book is relatively low if you already know the basics of SQL. All this makes reading on e-readers frustration free and a real joy.

The other thing I really like about this book is that it acts as a bit of a SQL refresher (in fact so far my only suggestion would have been to run with this and cover more SQL and how to do it with Ecto - though I’m guessing the remainder of the book may do some of that anyway).

The other reason I like this book is because it (perhaps obviously) showcases what’s so great about Ecto. Ecto has been put together very nicely and I like that it follows the Elixir theme of keeping magic to a minimum. The query syntax is pretty close to SQL query syntax, and I think this will make converting SQL to Ecto a lot easier than trying to do the same in other DB libraries or ORMs such as Active Record. It will also mean if you ever need to drop into raw SQL it should be a bit easier.

So my thoughts so far are… get this book!! It’s so incredibly easy to read that I’m pretty sure you’ll enjoy it just as much as I am!

Well done Eric and Darin! :023:

5 Likes

My review - continued…

I’ve now finished this book! So here’s the rest of my review :003:

I’ll start by saying that I wasn’t really sure what I was expecting from this book - I mean, how deep can a database library be? Or how involved might it be using one? Of course that was based on my experience with ActiveRecord. Well it turns out a DB library can be more than what you may be used to or expect, and I was very pleasantly surprised by this book as well as Ecto itself.

One of the things I’ve said numerous times about Elixir - that it and the community show us a ‘better way’ - is most definitely echoed in this book! Some of the things I learned that I didn’t know before are that I can create custom Ecto types, or use table-less schemas, or that polymorphism can be handled in several different ways - all with different pros and cons.

I also learned about upserts and transactions and as I mentioned in the book club thread, there was a very welcomed chapter on application design which covers separating the Pure from the Impure, (Phoenix) Contexts and Umbrellas! I really wasn’t expecting to see that in a book about Ecto and it was definitely very much appreciated.

I also enjoyed the chapter and details regarding Phoenix, and I agree with @peerreynders that it may actually be beneficial to read this book before Programming Phoenix (or as I did, after the initial part of it) - it is so easy to read and doesn’t require much cognitive load if you are already familiar with the basics of SQL and something like ActiveRecord, so you will finish it in no time!

Miss this book at your peril! It will show you why Ecto is different… and why Ecto guides you down a ‘better’ path. Isn’t that in part why we came to Elixir?

Well done @darinwilson and @ericmj (and for all your work on Ecto!) :023:

5 Likes

Nice review; now I’m even more excited to dig into this book after I revisit Programming Elixir >= 1.6 and work through Elixir in Action. :tada:

Thanks for reiterating this point; I’m going to follow suit. :spades: :diamonds: :clubs: :purple_heart:

2 Likes

Nice one @Ted! Let us know how you get on :003:

You have selected great books so you can’t go wrong no matter which order you may end up reading them :023:

I started a related thread here so as to not go off-topic in this thread :smiley:

I started reading the book and I agree with @AstonJ the book is very easy to follow :point_up: (I am far from being a DB expert) knowing how important Ecto in the elixir ecosystem reading it is a no brainer for anyone learning elixir or phoenix

This is my favorite elixir book so far :heart:

Thank you to the authors @darinwilson @ericmj and to everyone involved in this project!

4 Likes

This is my next book.

2 Likes

Just wanted to stated that I enjoyed this book a lot. :grinning:
Thank you @ericmj and @darinwilson for putting this together.

Favourite aspects of the book.

• I liked that Part I - Ecto Fundamentals encourages the reader to test and play with all the key features of Ecto. It made the book easy to jump into and kept me focused on first playing and testing things out. The ability to “play” is a key feature for learning.

• A lot of knowledge about Querying, Changesets, Schemas before even dealing with migrations. Thinking about structure, tables, columns and table joins later on in the book was a good move.

• Appreciate the ability to always reset the test data in the demo app.

Part II - Optimizing IEx for Ecto. I had no clue you can do that. Huge time saving tip for customizing IEx.

• Embedded schemas. Did not realize some of the things you can do with embedded schemas.

• Performing upserts with :on_conflict chapter. This now makes a lot of sense.

Whether your a beginner or an expert in Elixir this book is easy and fun to go through. I have already started refactoring some code on a current project with the new knowledge I have on Ecto.

Highly recommend the book. :+1:

5 Likes

Thanks for the nice feedback @neuone - I’m glad you found the book helpful!

1 Like

When running snippets in the programming ecto code project, I keep getting a strange error message:

iex(1)> Repo.insert(%Genre{name: "speed polka"})
# {:ok, %MusicDB.Genre{...}}

iex(2)> Repo.insert(%Genre{name: "speed polka"})
# ** (MatchError) no match of right hand side value: []

If I run code via the playground module, then I get the actual error message.

In a different project I get proper ecto error messages when in IEX shell. Can anyone suggest what might be going on?

@darinwilson any thoughts on this ? :point_up:

To clarify, all DB level errors are returned as this:

** (MatchError) no match of right hand side value: []

So the first insert works fine, but the second fails a unique constraint but returns an unusual error message.

To reiterate, any failing operation that I run in the playground, it correctly returns the standard error message.


Another example:

iex(5)> Repo.insert(%MusicDB.Artist{})
** (MatchError) no match of right hand side value: []
# 13:01:08.566 [debug] QUERY ERROR db=0.7ms queue=3.1ms
# INSERT INTO "artists" ("inserted_at","updated_at") VALUES ($1,$2) RETURNING "id" [~N[2020-08-09 11:01:08], ~N[2020-08-09 11:01:08]]

Hi @jmurphyweb :wave:

What version of Elixir/Erlang are you using? I tried the example using Elixir 1.9 and Erlang 21.3.7 and I got the expected behavior in IEx, i.e. it showed the full error message:

iex(3)> Repo.insert(%Genre{name: "speed polka"})

09:17:52.517 [debug] QUERY ERROR db=2.9ms queue=0.5ms
INSERT INTO "genres" ("name","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" ["speed polka", ~N[2020-08-10 16:17:52], ~N[2020-08-10 16:17:52]]
** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * genres_name_index (unique_constraint)
...

If you’re on Elixir 1.10, maybe something changed in IEx that’s triggering different behavior. Or is it possible that you have a global .iex.exs that’s modifying the behavior? I’m a little stumped, so I’m just guessing here… :confused:

2 Likes

I dropped down to elixir 1.9 and ran it but had the same result.
I’ve never set up a global .iex.exs file.

I’ll try a fresh install of the project and re-run it.


Fresh install works fine. No idea what I did on the old one… Thanks @darinwilson


Strangely enough when I moved the project back into my programming-ecto subdirectory the problem re-occurred. Moving the project back out and all is well again. I can’t understand why that happens but happy to ignore now it’s working again :+1:

Huh - that’s really strange.

Well, I guess it’s not quite “mystery solved” but I’m glad it’s at least “mystery REsolved” :smile:

1 Like

Encounter the problem when I run the seed.exs

Interestingly, it looks like the data were loaded few steps before the seed (probably, automatically when I invoke ecto create?)
So I’m able to count the loaded records from console

iex(1)> MusicDB.Repo.aggregate("artists", :count, :id)
22:56:04.664 [debug] QUERY OK source="artists" db=6.0ms decode=4.0ms queue=7.8ms idle=449.8ms
SELECT count(a0."id") FROM "artists" AS a0 []
3

Hence the message signals I’m inserting there duplicates? Or what?


$ mix run priv/repo/seeds.exs 
warning: use Mix.Config is deprecated. Use the Config module instead
  config/config.exs:11

warning: use Mix.Config is deprecated. Use the Config module instead
  config/dev.exs:9


22:31:28.937 [debug] QUERY ERROR db=24.7ms queue=4.3ms idle=10.2ms
INSERT INTO "genres" ("name","wiki_tag","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["jazz", "Jazz", ~N[2022-11-17 19:31:28], ~N[2022-11-17 19:31:28]]
** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * genres_name_index (unique_constraint)

If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `unique_constraint/3` on your changeset with the constraint
`:name` as an option.

The changeset has not defined any constraint.

    (ecto 3.9.1) lib/ecto/repo/schema.ex:795: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
    (elixir 1.14.2) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
    (ecto 3.9.1) lib/ecto/repo/schema.ex:780: Ecto.Repo.Schema.constraints_to_errors/3
    (ecto 3.9.1) lib/ecto/repo/schema.ex:761: Ecto.Repo.Schema.apply/4
    (ecto 3.9.1) lib/ecto/repo/schema.ex:369: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
    (ecto 3.9.1) lib/ecto/repo/schema.ex:265: Ecto.Repo.Schema.insert!/4
    priv/repo/seeds.exs:12: (file)