"Northwind Elixir Traders" updated: using cast_assoc and put_assoc in Ecto

“Northwind Elixir Traders” updated: using cast_assoc and put_assoc in Ecto

29 April, 2024 2 min read
software, programming, Elixir, learning, Ecto, databases, book

This update to the “Northwind Elixir Traders” book is short (compared to recent updates) but important, nonetheless.

Besides fixing the misnomer “lazy loading” that I used to mischaracterize Ecto’s non-automatic loading behavior of associations across Chapter 8, the new version 0.25.3 includes Chapter 9 on using the cast_assoc/3 and put_assoc/3 functions of Ecto.Changeset to work with associations either in whole or piecemeal.

This chapter was a classic case of going down an unproductive blind alley by following an exception error message too closely, and then backtracking to understand how things work. I must admit that working with associations without directly setting foreign-key values is both one of the most useful and among the most frustrating things I’ve ever faced while learning and using Ecto.

In the next chapter (due sometime within the coming week) we will finally import some data from the original Northwind Traders SQLite database into the Products, Suppliers and Categories table of Northwind Elixir Traders. To do so, we will learn about accessing a new SQLite database dynamically through the Repo. With the new and plentiful toy data of these three tables, we will examine simple queries and the new options that are unlocked in our schemas.