Approach #2: Using an Abstract Schema

This approach might not be completely intuitive at first glance, but it’s worth exploring as it reveals some interesting features about schemas that we haven’t looked at before, and might be the right solution for your app.

With this approach, we’ll create separate notes tables for each of the other tables we’re associating with. So we’ll have one notes table for artists, another for albums, and so on. Here’s how we might write the migration:

  create table(​:notes_for_artists​) ​do
  add ​:note​, ​:text​, ​null:​ false
  add ​:author​, ​:string​, ​null:​ false
  add ​:assoc_id​, references(​:artists​)
  timestamps()
  end

Get Programming Ecto now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.