"Northwind Elixir Traders" updated: basic table associations in Ecto

“Northwind Elixir Traders” updated: basic table associations in Ecto

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

After a very intense Sunday, I bring you another chapter of the “Northwind Elixir Traders” book : Chapter 7 – Basic Table Associations (and the chapters are now numbered).

This has been tons of fun and insightful to write! The book now also has a preface, for those more philosophically-inclined.

I’ll try something new this time: here’s an excerpt from the new chapter’s “Summary and outlook” closing paragraphs (every chapter gets one).

In this chapter, we learned about the one-to-many association, how it’s implemented with a foreign key column, and how it’s accomplished within SQLite3.

We also explored numeric types for database columns, how SQLite handles them, and how we should utilized them even if we don’t use PostgreSQL. Plus, we explored SQLite’s storage classes, type affinities, and the peculiarities of its dynamic typing capabilities, compared to stricter databases. We also saw that this does not impact our application.

In small “side quests”, we explored practical aspects of modeling numeric values, including the possibility of using integers for storing money amounts. We resisted perfectionism and yak-shaving, and focused on learning more about the focus topic of this chapter. Meanwhile, we did not forget the numerous opportunities for improvement that arise, not only to the Products and Suppliers tables of the Northwind Traders ERD, but also across many of its tables.

Finally, we learned about foreign key constraints, how they work in SQLite, and how SQLite limits our use of Ecto’s validation function of foreign key constraints. Regardless, we forged ahead and circumvented this limitation through the definition of a robust custom validation function.

Overall, we faced quite a few challenges along the way, but forged ahead by checking the helpful documentation of Ecto, Ecto SQLite, SQLite3, and PostgreSQL.

We have come very far, and haven’t yet even scratched the surface of associations with Ecto! For example, we have not yet utilized Ecto.Schema’s features for modeling the “belongs to” and “has many” ways of describing an association. For sure, it would be nice, for example, if we could access the data of the Category associated with a Product record, from within the Product record itself.

This is what we’ll be exploring in the next chapter.