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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #hex
- #security
- #metaprogramming











Showing Posts 11 to 20- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
Constraints are only tested after you do a repository operation, such as Repo.insert, since it is the database reporting what is wrong (rather than we testing it).
kostonstyle
So, I have to do Repo.insert to get to know if there is a constraint error or not?
josevalim
Yes, that’s by definition how constraints in Ecto work. They rely on the
database as the da abase is the only one who can effectively say if that
relationship exists or not.
kostonstyle
My last question(hopefully
), when I tried to insert into db, I’ve got an exception:
How to get a message instead raised exception?
In doc Ecto.Repo — Ecto v3.14.0 it says, I should get a tuple.
Thanks
josevalim
Can you post the full message? The error message is telling exactly what you need to do. You need to pass the :name option to
foreign_key_constraint/3with the foreign key name that is failing.kostonstyle
I’ve found the solution:
It works as expected.
At last, what is the :name options for, that I can pass?
kostonstyle
with the
nameoption, I’ve got error as I mentioned above.kostonstyle
So I will post the final code, that works.
Look at the function
foreign_key_constraint, I do not pass thenameoption parameter and it works. Why? What is the use of thenameoption parameter?josevalim
The name option would be given to the foreign_key_constraint function. If it works without passing the
:nameoption, it means Ecto is guessing the proper name for you.kostonstyle
So, when I what to pass the
:nameoption, how do I have to write?The migration of countries looks as follow:
Thanks