Skip to content

Commit

Permalink
fix: adding unique constraint for tag name
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaugher committed May 26, 2023
1 parent 592e449 commit 58660b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ npm-debug.log
yarn.lock
MnesiaCore.nonode*
Mnesia.nonode*
.DS_Store
5 changes: 3 additions & 2 deletions lib/tags_multi_tenant/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ defmodule TagsMultiTenant.Tag do
import Ecto.Changeset

schema "tags" do
field :name, :string
field(:name, :string)

has_many :taggings, TagsMultiTenant.Tagging
has_many(:taggings, TagsMultiTenant.Tagging)
end

def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name])
|> unique_constraint(:unique_tag_name, name: :tags_name_key)
|> validate_required(:name)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule TagsMultiTenant.Repo.Migrations.UpdateTagUniqueIndex do
use Ecto.Migration

def change do
drop_if_exists(unique_index(:tags, [:name], name: :tags_name_key))

create_if_not_exists(unique_index(:tags, [:name], name: :tags_name_key))
end
end

0 comments on commit 58660b9

Please sign in to comment.