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
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
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
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
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
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 AM → AD, 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 ![]()
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









