elixir - Build has_one relationship -
i have schemas, looks follow:
defmodule busiket.languagecode use busiket.web, :model schema "languages_code" field :code, :string field :text, :string timestamps end end
the second schema:
defmodule busiket.countrycode use busiket.web, :model schema "countries_code" field :alpha2, :string field :alpha3, :string timestamps end end
and third table
defmodule busiket.country use busiket.web, :model alias busiket.languagecode alias busiket.countrycode schema "countries" has_one :code, countrycode has_one :lang, languagecode field :text, :string timestamps end end
as can see on third schema, field code
should depends on country_code
schema field code
.
the lang field should depends on language_code
schema field alpha2
.
i not know, if schema country designed?
the entries in countries should looks like:
"ch" | "en" | "switzerland" "de" | "en" | "germany"
and record should faild:
"yy" | "en" | "foo"
because there no country yy
iso code.
the migration file language_code
looks follow:
defmodule busiket.repo.migrations.createlanguagecode use ecto.migration def change create table(:languages_code) add :code, :string, size: 3 add :text, :string timestamps end end end
and country_code
defmodule busiket.repo.migrations.createcountrycode use ecto.migration def change create table(:countries_code) add :alpha2, :string, size: 2 add :alpha3, :string, size: 3 timestamps end end end
and @ last tried country
migration:
defmodule busiket.repo.migrations.createcountrytable use ecto.migration def change create table(:countries) 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
i hope, clear want reach.
update
i created table sad:
defmodule busiket.repo.migrations.createcountrytable use ecto.migration def change create table(:countries) 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
when execute mix ecto.migrate, i've got following error:
20:34:11.012 [info] create table countries ** (postgrex.error) error (invalid_foreign_key): there no unique constraint matching given keys referenced table "countries_code"
i think, have change :alpha3 not unique.
two things:
you want
belongs_to
incountry
sincecountry
's table contains foreign keys. need specify foreign table's column name inbelongs_to
.schema "countries" belongs_to :code, countrycode, foreign_key: :alpha2 belongs_to :lang, languagecode, foreign_key: :code ... end
the
has_one
declarations should incountrycode
,languagecode
schemas.the option want specify in migration
column
, , should in callreferences
. (there noname
option inadd
, current code using.) need specifytype
.create table(:countries) add :code, references(:countries_code, column: :alpha2, type: :string) add :lang, references(:languages_code, column: :code, type: :string) ... end
Comments
Post a Comment