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:

  1. you want belongs_to in country since country's table contains foreign keys. need specify foreign table's column name in belongs_to.

    schema "countries"   belongs_to :code, countrycode, foreign_key: :alpha2   belongs_to :lang, languagecode, foreign_key: :code   ... end 

    the has_one declarations should in countrycode , languagecode schemas.

  2. the option want specify in migration column, , should in call references. (there no name option in add, current code using.) need specify type.

    create table(:countries)   add :code, references(:countries_code, column: :alpha2, type: :string)   add :lang, references(:languages_code, column: :code, type: :string)   ... end 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -