Add unique constraint to Translation

To make sure only one translation per article is possible.
This commit is contained in:
Thelonius Kort
2022-12-30 21:16:06 +01:00
parent d9022fb0c7
commit 2075315721
2 changed files with 10 additions and 0 deletions

View File

@ -26,5 +26,8 @@ defmodule Outlook.Translations.Translation do
|> cast(attrs, [:lang, :title, :teaser, :date, :public, :unauthorized])
|> cast(attrs, [:content], force_changes: true)
|> validate_required([:lang, :title, :teaser, :content, :date, :public, :unauthorized])
|> unique_constraint([:lang, :article_id],
message: "translation for this language already exists",
name: :article_id_lang_unique_index)
end
end

View File

@ -0,0 +1,7 @@
defmodule Outlook.Repo.Migrations.AddLangArticleIdUniqueIndexToTranslations do
use Ecto.Migration
def change do
create unique_index(:translations, [:article_id, :lang], name: :article_id_lang_unique_index)
end
end