add container tags

This commit is contained in:
shibao 2021-09-12 19:27:18 -04:00 committed by oliviasculley
parent f12e71cbe2
commit 48691e669e
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,31 @@
defmodule Cannery.Containers.ContainerTag do
use Ecto.Schema
import Ecto.Changeset
alias Cannery.{Containers, Tags}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "container_tags" do
belongs_to :container, Containers.Container
belongs_to :tag, Tags.Tag
timestamps()
end
@type t :: %{
id: Ecto.UUID.t(),
container: Containers.Container.t(),
container_id: Ecto.UUID.t(),
tag: Tags.Tag.t(),
tag_id: Ecto.UUID.t(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@doc false
def changeset(container_tag, attrs) do
container_tag
|> cast(attrs, [:tag_id, :container_id])
|> validate_required([:tag_id, :container_id])
end
end

View File

@ -0,0 +1,17 @@
defmodule Cannery.Repo.Migrations.CreateContainerTags do
use Ecto.Migration
def change do
create table(:container_tags, primary_key: false) do
add :id, :binary_id, primary_key: true
add :container_id, references(:containers, on_delete: :delete_all, type: :binary_id)
add :tag_id, references(:tags, on_delete: :delete_all, type: :binary_id)
timestamps()
end
create index(:container_tags, [:container_id])
create index(:container_tags, [:tag_id])
end
end