nova-sortable
nova-sortable copied to clipboard
Error when sorting on unique db index
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:
- Start transaction
- Move the entry to the end (max possible sorting value)
- Reorder the other entries if needed
- Assign the now free order value to the entry from (2)
- Commit transaction
Using a transaction will prevent users from seeing an incorrect ordering in step (2)
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
I will try to have a look into this and maybe create a PR on the weekend.