Associate artists and records

This commit is contained in:
Claudio Ortolina
2024-09-12 14:51:10 +01:00
parent e08fbe3740
commit e4ae157275
4 changed files with 44 additions and 0 deletions
+4
View File
@@ -2,6 +2,8 @@ defmodule MusicLibrary.Records.Artist do
use Ecto.Schema
import Ecto.Changeset
alias MusicLibrary.{Records.ArtistRecord, Records.Record}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "artists" do
@@ -9,6 +11,8 @@ defmodule MusicLibrary.Records.Artist do
field :image, :string
field :musicbrainz_id, Ecto.UUID
many_to_many :records, Record, join_through: ArtistRecord
timestamps(type: :utc_datetime)
end
@@ -0,0 +1,20 @@
defmodule MusicLibrary.Records.ArtistRecord do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "artists_records" do
field :artist_id, :binary_id
field :record_id, :binary_id
timestamps(type: :utc_datetime)
end
@doc false
def changeset(artist_record, attrs) do
artist_record
|> cast(attrs, [])
|> validate_required([])
end
end
+4
View File
@@ -2,6 +2,8 @@ defmodule MusicLibrary.Records.Record do
use Ecto.Schema
import Ecto.Changeset
alias MusicLibrary.{Records.Artist, Records.ArtistRecord}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "records" do
@@ -12,6 +14,8 @@ defmodule MusicLibrary.Records.Record do
field :musicbrainz_id, Ecto.UUID
field :genres, {:array, :string}
many_to_many :artists, Artist, join_through: ArtistRecord
timestamps(type: :utc_datetime)
end
@@ -0,0 +1,16 @@
defmodule MusicLibrary.Repo.Migrations.CreateArtistsRecords do
use Ecto.Migration
def change do
create table(:artists_records, primary_key: false) do
add :id, :binary_id, primary_key: true
add :artist_id, references(:artists, on_delete: :nothing, type: :binary_id)
add :record_id, references(:records, on_delete: :nothing, type: :binary_id)
timestamps(type: :utc_datetime)
end
create index(:artists_records, [:artist_id])
create index(:artists_records, [:record_id])
end
end