Ecto cast_embed is not detecting reordering of chidlren

We have an issue with an Embedded schema (Car) containing a list of Embedded Schemas (Images). When we add or delete images everything is working fine. Now we added Reordering the images using drag’n drop in the Frontend. But these reordering changes are not detected by cast_embed and the order stays the same.

A workaround that works is changing a field on Image (we have a version field, that can be changed). In that case the new order is persisted.

Any idea how to accomplish this reordering without doing hacky workarounds?

Ecto version is 2.1.6 (upgrading is hard right now, since it breaks half of our app, but could be a solution as well)


You should consider adding a position field to the images and order by that. I remember that years ago jQuery had a plugin to reorder elements based on a field as well. So should be well-covered by a frontend library.


Yeah, we thought about that. I guess that is probably the mist stable solution. But it feels odd to put an field to save the order in an ordered list. Right now we just increment a version number when changing order and that works as well.

It is a bit odd indeed but look at it this way: with a position field you will cement the feature and it will always work properly no matter what. :023:

1 Like

Sorry to bump this old thread, but the issue is still standing. Both JSON (the backing storage in Postgres) and lists have ordering as part of their semantics, adding a position field is redundant and error prone. This looks like a bug from where I’m looking, but maybe I’m missing something in Ecto (config?) that would make changes to the order of embedded documents persist?

Do you use a recent version of ecto? This has been updated so embeds do allow for ordering to be detected:


I thought I did… Thanks for the link, wasn’t able to get to that by googling. Bumping some minors fixed the issue :+1: