Last active
December 17, 2016 18:49
-
-
Save spalenza/9eaa229339c3021e46232e4f18386f22 to your computer and use it in GitHub Desktop.
Ecto Migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def up do | |
alter table(:videos) do | |
modify :user_id, references(:users, on_delete: :delete_all) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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