Useful Migrations commands in Phoenix

Migrations

Make a field nullable

mix ecto.gen.migration add_nullable_to_field

def change do
alter table(:events) do
modify(:title, :string, null: true, from: :string) # Title column is now nullable
end
end

Add field to existing table

mix ecto.gen.migration add_is_super_admin_to_merchant

Add references to existing table

mix ecto.gen.migration add_district_id_to_room

defmodule Tailwind.Repo.Migrations.AddedDistrictIdToRoom do   use Ecto.Migration
def change do
alter table :rooms do
add :district_id, references( :districts, on_delete: :delete_all)
end
end
end

Add Unique index to a field

I forgot to add unique constraint in email. so generating a migration to add constraint in email.

mix ecto.gen.migration add_uniq_constraint_to_email_on_merchant

Then do mix ecto.migrate

if you got error like “Key (email)=(abc@abc.com) is duplicated.”, that means you already have multiple values in database of same email. Remove those duplicate values and run the migrate command again.

We also need to add unique constraint for email in model .

--

--

--

Depths of ocean could not swallow me.

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

Recommended from Medium

10-Minute Tutorial: How to Convert a PyTorch Model to TensorRT™

First Issue of 《Journal of Machine Learning》 is Online

Random forest Algorithm With Python

Deep (learning) like Jacques Cousteau — Part 2 — Scalars

DBSCAN Clustering Algorithm Implementation from scratch | Python

cover

A $70,000,000 Machine Learning Project! Blight Violations in the City of Detroit

Machine Learning for Urban Planning: Estimating Parking Capacity

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
Sis Ccr

Sis Ccr

Depths of ocean could not swallow me.

More from Medium

Embedding Browser into .NET App: DotNetBrowser or CefSharp?

CefSharp or DotNetBrowser

D365FO Infolog call stack

pattern making

JWTs: Not All Merry and Jolly