coen.bakker

coen.bakker

Best way of guaranteeing and updating ordering of items retrieved from database?

Let’s say you have a one-to-many relationship between Day and Todo: i.e., one day has many todos. The order of the todos of a day is important. The todos must be retrieved from the database ordered in a specific way, that was determined when the todos were inserted into the database. Additionally, todos can be deleted, inserted and moved.

I have a similar setup in my application, at the moment. I rely on the order of certain items when they are retrieved from the database and I update their relative position whenever necessary. Currently, I update the positions of the items manually. When an items is, for example, deleted, all the items down the list shift their position with -1.

I was wondering if there is a better way of doing this. Possibly a solution that is built into Postgres or Ecto, for example. I am hoping to reduce the risk of problems with data integrity. I have been looking for a built-in solution, but have not been able to find confirmation that is does or does not exist.

Do you happen to know a better way?

Ty.

Marked As Solved

sodapopcan

sodapopcan

I think you’re asking the best way to manage the actual column dictating the order?

I think what you are doing is fine if you know you have relatively small collections. Otherwise, this is a really good article that discusses various solutions.

Also Liked

codeanpeace

codeanpeace

As of LiveView v0.19 and Ecto v3.10, dynamically adding, removing, and re-ordering child associated records can be nicely handled by using the new :sort_param option for Ecto’s cast_assoc/3 which pairs well with LiveView’s inputs_for.

outlog

outlog

also have a look at how this example does it:
https://github.com/chrismccord/todo_trek

believe it does the sorting using sortablejs using liveview hooks..

dimitarvp

dimitarvp

Not sure I understand OP well either but I’d think that using update_all with the inc option should work fine.

Example 1: Ecto.Repo — Ecto v3.14.0

Example 2: https://hexdocs.pm/ecto/composable-transactions-with-multi.html

Last Post!

smathy

smathy

Oh, I wanted to add, if you use B as the first element then you can always reposition in front of that first element, you’d just use A for the calculation but never assign it to an element, so you’d have no A but then potentially AMAD, AS etc.

Also, if you expanded the space a little to include 32 characters (or any power of 2, conveniently ?` - ?\s #⇒ 64 :), then you’d have easier divisions.

I still find the math very awkward for deciding how to find the midpoint between two strings, much more awkward than finding the midpoint between two decimals :slight_smile:

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement