Struggle with model relationships

I find it difficult to develop relationships with several models in my project.

Models: band, musician, instrument

Bands have several musicians

Musicians have several bands and several instruments.

It's all pretty simple, but I also need to keep track of what kind of instruments a musician has for a particular band. So, in a sense, I think bands have several instruments through musicians.

In the tables, I was going to add instrument_id to the band_musicians reference table, but I need a musician to have several instruments for the group, so I thought I would have to go to the musicians_instruments table.

What is the best way to establish relationships with these models?

Thank you for your time!

+3
source share
4 answers

Musicians would have a one-to-many relationship with both bands and instruments. So, create a table of your musicians and add all the information related to the musicians themselves to this table.

Create a tool table to store tool information and do the same for groups. This will take care of all of your individual elements.

Then create something like a "band_assignments" table, which has only the group identifier and the musician identifier and joins them together. Create a table 'instrument_assignment' to do the same.

, , , , , , .

5 .

musicians  (musician_id, first_name, last_name)
bands  (band_id, name)
instruments  (instrument_id, name)
band_instument_assignments  (musician_id, band_id, instrument_id, date_played)

, "band_instrument_assignments" - , . GROUP BY LIMIT , .

+1
+1

someoneinomaha , 4- , , . "Mus Model" ( ) , :

  • get_bands()
  • get_instruments()
  • get_musicians()
  • get_instruments_by_musician()
  • get_musicians_by_band()
  • get_instruments_by_band()
  • band_by_musician() .. , imho.
+1

I might be a little late for the party here and I'm not a database expert, but I found that extracting a DB schema helps a lot. Just create the fields and fill in the table names and columns, then draw arrows to define your relationships, and it should be much clearer how you should structure things and whether to add a table to join the other two tables.

If all else fails, simply copy the schema from databaseanswers.org. I am sure there is one that is likely to help you.

0
source

Source: https://habr.com/ru/post/1732951/


All Articles