Note: if using MySQL with a prefixed table, you must use the same prefix for the references since cross database references are not supported.
But I can’t find about Postgres. I have tried:
def change do
create table(:users) do
add :name, :string
add :role_id, references(:roles, prefix: :security)
timestamps()
end
create unique_index(:users, [:name])
end
And this returns:
** (Postgrex.Error) ERROR (undefined_table): relation "roles" does not exist
def change do
create table(:users) do
add :name, :string
add :role_id, :integer
timestamps()
end
execute "alter table users
add constraint users_role_id_fkey foreign key (role_id)
references security.roles (id)
match simple on update no action on delete no action"
create unique_index(:users, [:name])
create index(:users, [:role_id])
end
alter table users
add constraint users_role_id_fkey foreign key (role_id)
references security.roles (id)
match simple on update no action on delete no action
execute is not a ‘changeable’ command and will fail if rollback is run. Have to put an execute in up and an execute that reverses it in down (or nothing in down if it does not need to be reversed, have to only have it in up though).
defmodule App.Repo.Migrations.CreateSchema do
use Ecto.Migration
@schema_prefix Application.fetch_env!(:app, :schema_prefix)
def up do
execute "create schema #{@schema_prefix}"
end
def down do
execute "drop schema #{@schema_prefix}"
end
end
With FK, I guess it will be dropped with table during rollback.
You cannot do this in any Ecto public API.
I found that José Valimcommented:
References no longer support prefix because we cannot make it work across different database vendors. They always always point to something in the same prefix by default.