Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#13248 closed (fixed)

manage.py sqlsequencereset misses m2m tables

Reported by: Andrey Golovizin Owned by: Gabriel Hurley
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

Sequence reset code not generated for tables that are implicitly created for m2m fields.

Example models:

class User(models.Model):
    pass

class Group(models.Model):
    users = models.ManyToManyField(User)

The correct result of ./manage.py sqlsequencereset should be:

BEGIN;
SELECT setval('"example_user_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "example_user";
SELECT setval('"example_group_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "example_group";
SELECT setval('"example_group_users_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "example_group_users";
COMMIT;

Actual (incorrect) result:

BEGIN;
SELECT setval('"example_user_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "example_user";
SELECT setval('"example_group_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "example_group";
COMMIT;

Sequence example_group_users_id_seq is ignored here.

Attachments (1)

13248_sqlsequencereset.diff (671 bytes) - added by Gabriel Hurley 7 years ago.
sets include_auto_created to True for sqlsequencereset

Download all attachments as: .zip

Change History (7)

comment:1 Changed 7 years ago by Andrey Golovizin

Database backend is postgresql_psycopg2.

comment:2 Changed 7 years ago by Ramiro Morales

milestone: 1.2
Triage Stage: UnreviewedAccepted

Bisection between 1.1 (r11366) and current trunk tip (r12889) shows this was introduced in r11710. Accepting and setting target to 1.2 as this is a regression.

comment:3 Changed 7 years ago by Alex Gaynor

Almost definitely just a matter of passing auto=True (or whatever the kwarg is named) to cache.get_models()

Changed 7 years ago by Gabriel Hurley

Attachment: 13248_sqlsequencereset.diff added

sets include_auto_created to True for sqlsequencereset

comment:4 Changed 7 years ago by Gabriel Hurley

Has patch: set
Needs tests: set
Owner: changed from nobody to Gabriel Hurley
Status: newassigned

I've attached a patch which causes the auto-created models to be reset as they should, but for the life of me I can't figure out how to write a regression test for this...

If anyone has a bright idea on how to test without comparing SQL line-for-line (which will vary by backend) let me know.

comment:5 Changed 7 years ago by Russell Keith-Magee

Resolution: fixed
Status: assignedclosed

(In [12903]) Fixed #13248 -- Corrected the output of sqlsequencereset after the m2m changes. Thanks to EroSennin? for the report, and Gabriel Hurley for the fix.

comment:6 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top