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

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
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
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New

Other Trending Topics Top

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
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews