Creating Unique Constraint/Index Ecto.Migration: Elixir

Migration

create table(:movies) do
add(:genre, :string)
add(:title, :string)
timestamps()
end
create(
unique_index(
:movies,
~w(title genre)a,
name: :index_for_movies_duplicate_entries
)
)

Schema

defmodule Data.Schemas.Movie do
use Data.Schema
schema "movies" do
field(:genre, :string)
field(:title, :string)
timestamps()
end
@required ~w(title)a
@optional ~w(genre)a
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @optional ++ @required)
|> validate_required(@required)
|> unique_constraint(
:title,
name: :index_for_movies_duplicate_entries
)

end
end

Testing

--

--

--

www.alvinrapada.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Diary Of A Net Developer — Learning React

15 Parallax Scrolling Examples and Code

Pure CSS Parallax Scrolling

The content upload nightmare

✔✔ From JavaScript to TypeScript !!!

React Native+Redux+Realm.js (R3.js?) — A New Mobile Development Standard:

How to use Vue Lifecycle Hooks

[Action required] Your RSS.app Trial has Expired — Sat May 08 2021

Learning CSS — Part 0

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alvin Rapada

Alvin Rapada

www.alvinrapada.com

More from Medium

Native GET and SET operations on REDIS

Monorobot: a Slack bot for monorepos

Getting Started with Hot Chocolate .NET GraphQL Server

Use Existing Insomnia Collections with Monika to Monitor your APIs