krainboltgreene

krainboltgreene

Today I noticed that when defining a embeds_many the new struct has a default of [] which is confusing since:

  1. You can have jsonb[] columns with a default of NULL
  2. embeds_one has a default of nil
  3. You can’t define your own default!

I ask because I have code that expected it to behave like other fields and also because {} in postgres is twice as expensive as an null value in postgres:

ecto/lib/ecto/schema.ex at master · elixir-ecto/ecto · GitHub.

Looks like it was committed 7 years ago: Remove containers in favor of strategies · elixir-ecto/ecto@7f052d9 · GitHub

Showing Posts 1 to 10

cmo

cmo

Don’t see how this makes the default for embeds many confusing. What else could this value be? And a list of nothing is an empty list, no?

I get what you mean that it could say there is no value here, but that is if you think of the embed many field as the value. I think of the values as the things being embedded.

Using a list over nil probably saves us from nil checks all over the place. Would you prefer to check if it’s nil before you try to operate on it every time?

krainboltgreene

krainboltgreene OP

In every database I can find the default for a column that is a list is always null.

It’s also the default for every field and at least one of the two embeds_.

This stands out exclusively against common design and expectation.

Beyond the fact that it actually does impact the database (again double the space cost), there is a massive difference between “this column has no value” and “this column is an empty list”.

joey_the_snake

joey_the_snake

You could submit a proposal to the mailing list to make the default configurable and see what they say: https://groups.google.com/g/elixir-ecto.

Changing the default for everyone probably isn’t a good idea because it will break existing code.

I’m not sure why it is empty list but it might have something to do with the fact that embeds and associations are treated very similarly. And when you attempt to preload an association it will give an empty list when none are found. This makes sense because the result of the preload is defined as a list of associations and not a nullable column.

krainboltgreene

krainboltgreene OP

I’ll post there next. I wanted to do it here in case there was something I was missing.

krstfk

krstfk

I would argue this is the only correct default. Embeds many represents associated records of which there can be 0 or more.
The number of records in NULL would be undefined, which would be incorrect.

LostKobrakai

LostKobrakai

Embeds are modeled to align closely with assocs and for assocs empty set doesn‘t mean there‘s a column somewhere set to NULL, but it means there‘s no rows at all. It makes sense to default that field to empty list, because as mentioned before it‘s much cleaner not to check for nil and empty list everywhere.

joey_the_snake

joey_the_snake

I can see it both ways: thinking about it as a nullable column with a complex datatype or as denormalized associations.

josevalim

josevalim

Creator of Elixir

This is it. :slight_smile:

krstfk

krstfk

Ok, but embeds_many implies a collection of stuffs. I would say that if you have a complex type that might be null or a list, then you have one embed that might be null or a list, not a collection of things that might be in the list.

I’m not a not null radical ala CJ Date, and if you do want to have a complex type with a maybe list, model it as such (and it’s still probably not great). But if you call something embeds_many or has_many it implies a countable thing and NULL is not.

Then again op stated:

Which is plainly wrong.

joey_the_snake

joey_the_snake

Ok, but embeds_many implies a collection of stuffs. I would say that if you have a complex type that might be null or a list, then you have one embed that might be null or a list, not a collection of things that might be in the list.

That’s a good point. I guess there’s a bit of tension if someone wants to take advantage of embedded schemas for a list type but also wants to treat it as a nullable column.

There is sometimes value in allowing both NULL and empty list. The former might signal the data is missing while the latter might signal the data is complete and there is nothing to report.

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
515 12931 101
New
wmnnd
Hey folks, I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Ganna
Hi everyone, I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
AstonJ
A remark @Garrison made… ..has already inspired a thread for the technical side of reclaiming our internet, but there is of course anot...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews