如何用ecto实现双向外键
How to implement bidirectional foreign key with ecto
我是 elixir 编程的新手,我正在尝试迁移两个在两个表中都有外键的数据库,但它似乎不起作用??
def change do
create table(:users) do
add :username, :string
add :email, :string
add :name, :string
add :password, :string
add :address, :string
timestamps()
end
alter table("users") do
add :organization, references(:organization)
end
end
def change do
create table(:organization) do
add :org_key, :string
add :name, :string
timestamps()
end
create unique_index(:organization, [:org_key])
alter table("organization") do
add :creator, references(:users)
end
end
不清楚 why/whatfor 你是否有两种不同的迁移,但正如@sbacarob 在评论中所说,肯定不能引用前者 table :organizations
中不存在的。
没有魔法,代码是逐行执行的,不能在声明之前引用某些东西。以下最有可能起作用。
def change do
create table(:users) do
…
end
create table(:organizations) do
…
end
create unique_index(:organizations, [:org_key])
alter table("organizations") do
add :creator, references(:users)
end
alter table("users") do
add :organization, references(:organizations)
end
end
我是 elixir 编程的新手,我正在尝试迁移两个在两个表中都有外键的数据库,但它似乎不起作用??
def change do
create table(:users) do
add :username, :string
add :email, :string
add :name, :string
add :password, :string
add :address, :string
timestamps()
end
alter table("users") do
add :organization, references(:organization)
end
end
def change do
create table(:organization) do
add :org_key, :string
add :name, :string
timestamps()
end
create unique_index(:organization, [:org_key])
alter table("organization") do
add :creator, references(:users)
end
end
不清楚 why/whatfor 你是否有两种不同的迁移,但正如@sbacarob 在评论中所说,肯定不能引用前者 table :organizations
中不存在的。
没有魔法,代码是逐行执行的,不能在声明之前引用某些东西。以下最有可能起作用。
def change do
create table(:users) do
…
end
create table(:organizations) do
…
end
create unique_index(:organizations, [:org_key])
alter table("organizations") do
add :creator, references(:users)
end
alter table("users") do
add :organization, references(:organizations)
end
end