Skip to content

Instantly share code, notes, and snippets.

@spalenza
Last active December 17, 2016 18:49
Show Gist options
  • Save spalenza/9eaa229339c3021e46232e4f18386f22 to your computer and use it in GitHub Desktop.
Save spalenza/9eaa229339c3021e46232e4f18386f22 to your computer and use it in GitHub Desktop.
Ecto Migration
defmodule Rumbl.Repo.Migrations.CreateVideo do
use Ecto.Migration
def change do
create table(:videos) do
add :url, :string
add :title, :string
add :description, :text
add :user_id, references(:users, on_delete: :nothing)
timestamps()
end
create index(:videos, [:user_id])
end
end
def up do
alter table(:videos) do
modify :user_id, references(:users, on_delete: :delete_all)
end
end
defmodule Rumbl.Repo.Migrations.ChangeUserIdFromVideos do
use Ecto.Migration
def up do
execute "ALTER TABLE videos DROP CONSTRAINT videos_user_id_fkey"
alter table(:videos) do
modify :user_id, references(:users, on_delete: :delete_all)
end
end
def down do
execute "ALTER TABLE videos DROP CONSTRAINT videos_user_id_fkey"
alter table(:videos) do
modify :user_id, references(:users, on_delete: :nothing)
end
end
end
18:32:30.821 [info] alter table videos
** (Postgrex.Error) ERROR (duplicate_object): constraint "videos_user_id_fkey" for relation "videos" already exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment