第3のスキーマでは、フィールドcode
は、フィールドのcountry_code
スキーマに依存する必要があります210。ビルドにhas_one関係
langフィールドは、フィールドalpha2
を持つlanguage_code
スキーマに依存する必要があります。
スキーマの国がうまく設計されているかどうかわかりませんが、国における
エントリは次のようになります必要があります。
"CH" | "EN" | "Switzerland"
"DE" | "EN" | "Germany"
と、このレコードはfaild必要があります。
"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"
、私は変更する必要があります:アルファ3は一意ではありません。
どう 'CountryCode'と' LanguageCode'スキーマ内にhas_oneは次のようになります必要がありますでしょうか? –
私の投稿を更新しました。 –
'countries_code'の' alpha2'と 'languages_code'の' code'のユニークなインデックスを追加する必要があると思います。既存の移行を編集してこれらのフィールドを追加する場合は、必ず移行を再実行してください。 – Dogbert