1
我有模式,看起來如下:構建HAS_ONE關係
defmodule Busiket.LanguageCode do
use Busiket.Web, :model
schema "languages_code" do
field :code, :string
field :text, :string
timestamps
end
end
第二個模式:
defmodule Busiket.CountryCode do
use Busiket.Web, :model
schema "countries_code" do
field :alpha2, :string
field :alpha3, :string
timestamps
end
end
和第三臺
defmodule Busiket.Country do
use Busiket.Web, :model
alias Busiket.LanguageCode
alias Busiket.CountryCode
schema "countries" do
has_one :code, CountryCode
has_one :lang, LanguageCode
field :text, :string
timestamps
end
end
,你可以在看第三個模式,字段code
應取決於country_code
模式與字段code
。
lang字段應取決於language_code
架構與字段alpha2
。
我不知道,如果架構國家是精心設計的?
在國家中的條目應該是這樣的:
"CH" | "EN" | "Switzerland"
"DE" | "EN" | "Germany"
這個記錄應zhcon失敗:
"YY" | "EN" | "Foo"
因爲沒有與YY
ISO代碼的國家。
從language_code
遷移文件看起來如下:
defmodule Busiket.Repo.Migrations.CreateLanguageCode do
use Ecto.Migration
def change do
create table(:languages_code) do
add :code, :string, size: 3
add :text, :string
timestamps
end
end
end
和country_code
defmodule Busiket.Repo.Migrations.CreateCountryCode do
use Ecto.Migration
def change do
create table(:countries_code) do
add :alpha2, :string, size: 2
add :alpha3, :string, size: 3
timestamps
end
end
end
,最後我試着用country
遷移:
defmodule Busiket.Repo.Migrations.CreateCountryTable do
use Ecto.Migration
def change do
create table(:countries) do
add :code, references(:countries_code), [name: :alpha2]
add :lang, references(:languages_code), [name: :code]
add :text, :string
timestamps
create unique_index(:countries, [:code, :lang])
end
end
end
我希望,它是清楚我想要達到的目標。
UPDATE
我創建的表爲你傷心:
defmodule Busiket.Repo.Migrations.CreateCountryTable do
use Ecto.Migration
def change do
create table(:countries) do
add :coun, references(:countries_code, column: :alpha2, type: :string)
add :lang, references(:languages_code, column: :code, type: :string)
add :text, :string
timestamps
end
create unique_index(:countries, [:coun, :lang])
end
end
當我執行混合ecto.migrate,我有以下錯誤:
20:34:11.012 [info] create table countries
** (Postgrex.Error) ERROR (invalid_foreign_key): there is no unique constraint matching given keys for referenced table "countries_code"
我想,我必須改變:alpha3不是唯一的。
如何HAS_ONE在'CountryCode'和'LanguageCode'模式應該樣子? –
我更新了我的帖子。 –
我想你還需要在'countries_code'中爲'alpha2'添加一個唯一索引,並在'languages_code'中爲'code'添加一個唯一索引。如果您編輯現有遷移以添加這些字段,請務必重新運行遷移。 – Dogbert