kostonstyle
Hi all
I have following schemas
schema "languages_code" do
field :code, :string
field :text, :string
timestamps
end
and another schema that will be associated with languages_code schema:
schema "countries" do
belongs_to :code, CountryCode, references: :alpha2
belongs_to :language, LanguageCode, references: :code
field :text, :string
timestamps
end
def changeset(model, params \\ %{}) do
model
|> cast(params, [:text])
|> cast_assoc(:code)
|> cast_assoc(:language)
|> validate_required([:code, :language, :text])
end
Then I test it as follow:
iex(1)> alias Busiket.Country
Busiket.Country
iex(2)> v = %{code: "CH", language: "DE", text: "Schweiz"}
%{code: "CH", language: "DE", text: "Schweiz"}
iex(3)> c = Country.changeset(%Country{}, v)
#Ecto.Changeset<action: nil, changes: %{text: "Schweiz"},
errors: [language: {"is invalid", [type: :map]},
code: {"is invalid", [type: :map]}], data: #Busiket.Country<>, valid?: false>
iex(4)>
What is wrong with association?
Update
I tried as follow:
iex(4)> v = %{code: %{code: "CH"}, language: %{alpha2: "DE"}, text: "Schweiz"}
%{code: %{code: "CH"}, language: %{alpha2: "DE"}, text: "Schweiz"}
iex(5)> c = Country.changeset(%Country{}, v)
#Ecto.Changeset<action: nil,
changes: %{code: #Ecto.Changeset<action: :insert, changes: %{},
errors: [alpha2: {"can't be blank", []}, alpha3: {"can't be blank", []}],
data: #Busiket.CountryCode<>, valid?: false>,
language: #Ecto.Changeset<action: :insert, changes: %{},
errors: [code: {"can't be blank", []}, text: {"can't be blank", []}],
data: #Busiket.LanguageCode<>, valid?: false>, text: "Schweiz"}, errors: [],
data: #Busiket.Country<>, valid?: false>
I’ve forgot to mention, that the data on the language_code table is already available:
I do not have to insert it, only to validate, if the value of the field code of the country is match to the table of language_code.
Thanks
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
If you don’t need to insert it, just validate it exists, you can let the database take care of it for you:
Then you can query your database to find the name of the foreign keys. If you are not sure the foreign key exists, then you can try to insert a country with a code that certainly does not exist and Ecto will error, telling you which foreign key name that failed, or succeed, which means you have no primary keys and therefore you should add one.
kostonstyle
First of all, thanks for your answer.
Do I need anymore?
or just:
Thanks
josevalim
Sorry, you are right. You don’t need
cast_assocbut make sure you pass both:codeand:languageto cast. I will edit my previous answer for clarify.kostonstyle
Thanks jose.
josevalim
Did it work?
kostonstyle
Sorry.
I changed my schema to:
as output
It should complain, a country with code LK does not exists!
Thanks
kostonstyle
@josevalim I found the error, it is on migration file, since I rename the column:
I rename the column from
countocodeand fromlangtolanguage.Now how to add references to the new name of columns?
kostonstyle
Delete and create the countries database with:
and tried with:
The compiler should complain.
josevalim
If you are using “references”, one is automatically added for you. Please
check the answer in my first post, I think you did not add the new fields
to the “cast” call in your changeset function (because I forgot to do so as
well in my original reply).
kostonstyle
Hi jose
I tried again as you metioned and it does not work.
The schema and changeset function:
and migration:
def change do
When I look at the pgAdmin console, the migration did the right job:
as you can see on the picture, the constraints on
countriestable is available.I test as follow:
The country with iso
YYdoes not exists oncountries_codedb:What am I doing wrong?
Thanks.