Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to have optional values for an ecto embedded schema?

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)

Marked As Solved

dimitarvp

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.

Also Liked

KiKi

KiKi

No, you got it right, null: false is for regular schemas, there are also no migrations for embedded schemas.

LostKobrakai

LostKobrakai

Depending on the database you can use certain constraints for whatever column type is used to back :map. In postgres this would be json/jsonb column 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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement