aNerdInTheHand
How do I design data structures with multiple join tables?
Hi all, I’m learning Phoenix and building an application that creates musical chord progressions, but my relational data skills are a bit rusty (see my previous question).
I may not have phrased this question very well but I’m trying to achieve a data design something like the following:
- A
progressionhas multiple chords - A
chordcan belong to many progressions - A
chordhas oneextension - An
extensioncan belong to many chords
In my initial design I didn’t have a separate extension table - extension was a column on the chord table. I used a join table called progression_chords with the following schema:
create table(:progression_chords) do
add :progression_id, references(:progressions)
add :chord_id, references(:chords)
add :index, :integer
This works fine and allows me to create records in the progression_chords table that reference the id of both a progression and a chord. But now I want to further normalise my data and move extensions out into a new table. I suspect that chord should now also be a join table, and I should have a numerals table, creating a chord schema like:
create table(:chords) do
add :extension_id, references(:extensions)
add :numeral_id, references(:numerals)
So, eventually, onto my two questions:
- is this over-normalising my data?
- If I do create this relationship table for
chords, does that change how I referencechord_idinprogression_chords(i.e. can I reference a join table in a join table)?
Apologies if this is a bit rambling, happy to clarify if needed.
Marked As Solved
dimitarvp
No, that should be fine. Look into the many-to-many Ecto docs, they have examples that look similar to your code.
Also Liked
al2o3cr
Unless you’re doing some truly revolutionary music theory, making numerals a database table (versus an Enum) seems like over-normalization. What data is on that table?
dimitarvp
Still, have in mind I said “similar”, not 100%. ![]()
Search for many_to_many on this forum and you can find plenty of inspiration. People have stumbled upon it a good number of times and their threads are IMO illuminating.







