Approach #2: Use an Abstract Schema
Explore how to use abstract schemas in Ecto to create polymorphic associations by managing separate note tables for artists, albums, and tracks. Understand the advantages and challenges of this approach, including cleaner table design and the need to manage repeated columns across tables. Learn how to associate records without directly inserting notes, using build_assoc and other Ecto functions.
Create separate notes tables
This approach might not be entirely intuitive at first glance, but it’s worth exploring as it reveals some interesting features about schemas that we haven’t looked at before. It might also be the right solution for our application.
With this approach, we’ll create separate notes tables for each of the other tables we’re associating with. We’ll have one notes table for artists, another for albums, and so on. Here’s how we might write the migration:
Notice that we used assoc_id for the foreign key. We deliberately chose a more abstract name and used it in each of the tables. We did this so all of the tables have the same column names. This allows us to create a ...