jpwatts / django-positions

A Django field for custom model ordering.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Switching positions after query.

Uznick opened this issue · comments

Hi. I've had a problem while emplementing subj.

The problem is: i select model records from db using
from_db = SimpleModel.objects.only('name', 'position').filter(...).exclude(...).order_by('position')
And I need to set new position for both of them. Coincidently, they're just switching their places.
So, I write:
for i, control in enumerate(movable_controls):
control.position = i
control.save()
And in debugger I see strange queries:

  SELECT (1) AS "a"
  FROM "tbl1"
  WHERE "tbl1"."id" = 514 LIMIT 1;

  SELECT "tbl1"."id",
         "tbl1"."parent"
  FROM "tbl1"
  WHERE "tbl1"."id" = 514;

  SELECT COUNT(*)
  FROM "tbl1"
  WHERE "tbl1"."parent" = 9;

  UPDATE "tbl1"
  SET "parent" = 9,
      "position" = 0
  WHERE "tbl1"."id" = 514

And the same for second element.
And what's stranger, now we have records with duplicate position in database, their positions are not recalculated according to these ones.

So, the queries are strange, and there is a problem in recalculation of positions.

Now, I used raw sql to fix this, but it does not look good :)

And also, thanks for quick fix of #17a1f50 :)

I'm having a hard time following what's going on here. Is there any way you could provide a simple example model with a failing test case?