Hi all
I am trying to use ecto and understand the concept of belongs_to and has_many.
On github you can find the mix project.
When you are looking on module Relation.Country
the changeset function, I do not know if the code is ok or not.
Can someone please help me and tell me, if I am usingbelongs_to
and has_many
in the right way. Of course I could design the model in other way, but here I am trying to use belongs_to
and has_many
.
I tried to test with:
iex(4)> changeset = Country.changeset(%Country{}, %{ name: "United Kingdom", country_iso: "GB", language_iso: "en" })
#Ecto.Changeset<action: nil, changes: %{name: "United Kingdom"}, errors: [],
data: #Relation.Country<>, valid?: true>
iex(5)> alias Relation.Repo
Relation.Repo
iex(6)> Repo.insert! changeset
15:12:01.022 [debug] QUERY OK db=0.3ms
begin []
15:12:01.026 [debug] QUERY OK db=1.6ms
INSERT INTO "countries" ("name","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" ["United Kingdom", {{2016, 12, 4}, {14, 12, 1, 0}}, {{2016, 12, 4}, {14, 12, 1, 0}}]
15:12:01.094 [debug] QUERY OK db=67.1ms
commit []
%Relation.Country{__meta__: #Ecto.Schema.Metadata<:loaded, "countries">,
country_iso: #Ecto.Association.NotLoaded<association :country_iso is not loaded>,
country_iso_id: nil, id: 3, inserted_at: #Ecto.DateTime<2016-12-04 14:12:01>,
language_iso: #Ecto.Association.NotLoaded<association :language_iso is not loaded>,
language_iso_id: nil, name: "United Kingdom",
updated_at: #Ecto.DateTime<2016-12-04 14:12:01>}
And the record does not get inserted at all.
Thanks