nova-sortable icon indicating copy to clipboard operation
nova-sortable copied to clipboard

Error when sorting on unique db index

Open jacob418 opened this issue 3 years ago • 2 comments

In my Project I have a unique index on the order column. When reordering the entries I receive a 'duplicate key entry' error for the order column. While investigating the code I noticed that the reordering is not encapsulated within a db-transaction.

I understand that in some cases one might not want to use a transaction but in general I would really like to have the ability to decide weather to use a transaction or not. Also a transaction would solve the duplicate key error:

  1. Start transaction
  2. Move the entry to the end (max possible sorting value)
  3. Reorder the other entries if needed
  4. Assign the now free order value to the entry from (2)
  5. Commit transaction

Using a transaction will prevent users from seeing an incorrect ordering in step (2)

jacob418 avatar Aug 19 '21 15:08 jacob418

Transaction didn't seem to help, I wrapped the call in a transaction but it still throws the same error.

See this issue: https://github.com/spatie/eloquent-sortable/issues/44

Tarpsvo avatar Oct 01 '21 14:10 Tarpsvo

I will try to have a look into this and maybe create a PR on the weekend.

jacob418 avatar Oct 11 '21 08:10 jacob418