sqlrelational-database

How to maintain user-modifiable order of items in SQL


My application handles many-to-one relationships between packages and documents. A package contains multiple documents. They are currently stored in PostgreSQL database using tables package and document, where table document has a foreign key column package-id.

A new requirement calls for the user to be able to rearrange the order of the documents in a package, so I need to begin tracking this. Note that the sort order is not based on any property of the document, such as a date; it can be arbitrarily assigned and re-assigned by the user. I need to decide among options. The ones that seem not awful are:

This has to be a common scenario - what is the standard way to structure this?


Edit found a nice solution for generating strings that sort between other strings, to avoid collisions. Will use this together with a new sequence column in the documents table. Return a new string that sorts between two given strings


Solution

  • As discussed in the comments, only one sort is needed (e.g., the same sort applies to all users).

    Therefore, the easiest/simplest solution is to modify the documents table.

    However, there are multiple further issues to be decided

    Finally, in some implementations I've done, the sorting has been a kind of 'add-on' for a subset of data. In those cases, I've made a separate table (equivalent of package-id, document-id, sequence-number) to leave the original data pristine. But this is a stylistic/maintenance focussed solution rather than simplest. (Technically, I was making it the simplest to ignore and remove later, as nothing else would use this new table).