Fl4m3Ph03n1x
Background
I am trying to create an embedded schema that needs to save information about orders.
Sometimes orders have no customer_id and that is fine (for the application).
defmodule Order do
use Ecto.Schema
embedded_schema do
field(:customer_id, :integer | nil)
field(:stage, enum: [:bought | :in_shop_cart | :canceled])
field(:details, :string)
end
end
Problem
The issue here is that I get a compile error for customer_id:
(CompileError) misplaced operator |/2
The | operator is typically used between brackets as the cons operator:
[head | tail]
where head is a single element and the tail is the remaining of a list.
It is also used to update maps and structs, via the %{map | key: value} notation,
and in typespecs, such as @type and @spec, to express the union of two types
I understand the issue here is the use of :integer | nil. This is not a list, so the advice I get is not usable.
Question
How can I represent optional values in my embedded schema ? (specifically :something | nil)
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
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
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
KiKi
That’s it, you don’t have to use nil. If you want this field to be required by the database, you should add null: false.
dimitarvp
All fields are optional by default, you only make them mandatory through validations in a changeset.
Remove the invalid Elixir syntax and you’re good to go.
Fl4m3Ph03n1x
So for my scenario, the field
:detailsis nullable by default, correct?A fixed version would therefore be:
And then in the database columns, set
null: false. I will also need to validate this requirement in changeset validations as @dimitarvp pointed out, correct?KiKi
[quote=“Fl4m3Ph03n1x, post:4, topic:51836”]
I made a mistake in my previous answer, you should add null: false to your migration, and in schema, you can just skip it.
KiKi
Yes, use validate_required to validate the field is there.
Fl4m3Ph03n1x
In my migration?
I understand from the docs that the only thing I can do is the following:
From here:
I have a table called “action” and I am going to add a new column called “order” which will be the embedded schema I mention. I did not find anything that allows me to specify what fields can or cannot be
nullwhen defining the embedded schema or the migration.Have I missed something?
KiKi
No, you got it right, null: false is for regular schemas, there are also no migrations for embedded schemas.
LostKobrakai
Depending on the database you can use certain constraints for whatever column type is used to back
:map. In postgres this would bejson/jsonbcolumn and you can e.g. use check constraints to enforce properties on them.For a topic like this I’d strongly suggest looking at your used database docs just as much as at ecto.