in SQL, or Django ORM, what's the conventional way to have an ordered one-to-many?

前端 未结 5 667
暗喜
暗喜 2021-02-06 07:12

Say I wanted to have a project, and one-to-many with to-do items, and wanted to re-order the to-do items arbitrarily?

In the past, I\'ve added a numbered order field, a

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 07:48

    "added a numbered order field" - good.

    "update all the items with their new order numbers" - avoidable.

    Use numbers with gaps.

    • Floating point. That way, someone can insert "1.1" between 1 and 2. I find that this works nicely, as most people can understand how the sequencing works. And you don't have to worry too much about how much space to leave -- there's lots and lots of space between each number.

    • On the initial load, number the articles by the 100 or 1000 or something with space between each one. In this case, you have to guess how many digits to leave for reordering.

    • A comma-separated position. Initially, they're all (1,0), (2,0), (3,0), etc. But when you want to rearrange things, you might have to introduce (2,1) and (2,2) that go after (2,0) but before (3.0).

      This looks kind of complicated, but some people like this kind of complexity. It's essentially the same as floating-point, except the single number is replace by a (whole-number, implicit-fraction) tuple. And this extends to handle hierarchies.

提交回复
热议问题