Fl4m3Ph03n1x

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)

Showing Posts 1 to 8

KiKi

KiKi

field(:customer_id, :integer)

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

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

Fl4m3Ph03n1x OP

So for my scenario, the field :details is nullable by default, correct?

A fixed version would therefore be:

defmodule Order do
  use Ecto.Schema

  embedded_schema do
    field(:customer_id, :integer)
    field(:stage, enum: [:bought | :in_shop_cart | :canceled])
    field(:details, :string)
  end
end

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

KiKi

[quote=“Fl4m3Ph03n1x, post:4, topic:51836”]

defmodule Order do
  use Ecto.Schema

  embedded_schema do
    field(:customer_id, :integer)
    field(:stage, enum: [:bought | :in_shop_cart | :canceled])
    field(:details, :string)
  end
end

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

KiKi

Yes, use validate_required to validate the field is there.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

In my migration?

I understand from the docs that the only thing I can do is the following:

defmodule MyApp.Repo.Migrations.AddOrderToAction do
  use Ecto.Migration

  def change do
    alter table("action") do
      add(:order, :map)
    end
  end
end

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 null when defining the embedded schema or the migration.

Have I missed something?

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
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
kszambelanczyk
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
Onor.io
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
Trolleger
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
RemyXRenard
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
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews