Hi all
I have schemas, that looks as follow:
defmodule Busiket.LanguageCode do
use Busiket.Web, :model
schema "languages_code" do
field :code, :string
field :text, :string
timestamps
end
end
the second schema:
defmodule Busiket.CountryCode do
use Busiket.Web, :model
schema "countries_code" do
field :alpha2, :string
field :alpha3, :string
timestamps
end
end
and the third table
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
as you can see on the third schema, the field code
should depends on country_code
schema with field code
.
The lang field should depends on language_code
schema with field alpha2
.
I do not know, if the schema country is well designed?
The entries in the countries should looks like:
"CH" | "EN" | "Switzerland"
"DE" | "EN" | "Germany"
and this record should failed:
"YY" | "EN" | "Foo"
because there is no a country with YY
iso code.
The migration file from language_code
looks as follow:
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
and 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
and at last I tried with country
migration:
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
I hope, it is clear what I want to reach.
Thanks