Opened 6 years ago

Closed 4 years ago

#15076 closed Bug (fixed)

inspectdb to quote ForeignKey class names to avoid need to reorder classes/solve circular dependencies

Reported by: saschwarz Owned by: nobody
Component: Core (Management commands) Version: 1.2
Severity: Normal Keywords: inspectdb
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The inspectdb management command supplies either "self" or the unquoted name of foreign key's class as the first argument to the ForeignKey constructor. This requires that classes be defined prior to their first use. If the class name were supplied as a quoted string the ordering of classes isn't required. That would also solve the case where circular dependencies keep classes from being defined in an appropriate order.

I've attached the trivial changes as a diff from trunk 15194.

Attachments (3)

inspectdb_15194.diff (833 bytes) - added by saschwarz 6 years ago.
django-fk-quoting.diff (1.3 KB) - added by jeff@… 6 years ago.
Quote ForeignKey models only if the model has not appeared yet.
15194-3.diff (2.3 KB) - added by Claude Paroz 5 years ago.
Identical patch + updated test

Download all attachments as: .zip

Change History (15)

Changed 6 years ago by saschwarz

Attachment: inspectdb_15194.diff added

comment:1 Changed 6 years ago by Ramiro Morales

Component: UncategorizedORM aggregation
Needs documentation: unset
Needs tests: unset
Owner: nobody deleted
Patch needs improvement: unset
Triage Stage: UnreviewedDesign decision needed

If I remember correctly, specifying the FK destination model name inside quotes doesn't work well when the model is already known, so a general solution like this (always using quoting) can result in faulty introspected models.py's.

Version 0, edited 6 years ago by Ramiro Morales (next)

comment:2 Changed 6 years ago by Ramiro Morales

Component: ORM aggregationdjango-admin.py inspectdb
Owner: set to nobody

Changed 6 years ago by jeff@…

Attachment: django-fk-quoting.diff added

Quote ForeignKey models only if the model has not appeared yet.

comment:3 Changed 6 years ago by jeff@…

Please see attached patch; it only quotes model names if the model has not yet been written out. Is that an acceptable deal? It should prevent quoting known model names but still quote model names that have not been initialized yet.

comment:4 Changed 6 years ago by Gabriel Hurley

Component: django-admin.py inspectdbCore (Management commands)

comment:5 Changed 5 years ago by James Addison

Severity: Normal
Type: Bug

comment:6 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset
UI/UX: unset

#16476 was a duplicate. And yes, it sounds like a good idea.

comment:7 Changed 5 years ago by Ramiro Morales

Keywords: inspectdb added

comment:8 Changed 5 years ago by Ramiro Morales

Needs tests: set

comment:9 Changed 5 years ago by Ramiro Morales

Triage Stage: Design decision neededAccepted

#9164 was a duplicate.

Changed 5 years ago by Claude Paroz

Attachment: 15194-3.diff added

Identical patch + updated test

comment:10 Changed 5 years ago by Claude Paroz

Has patch: set
Needs tests: unset

comment:11 Changed 4 years ago by Ramiro Morales

Triage Stage: AcceptedReady for checkin

comment:12 Changed 4 years ago by Claude Paroz

Resolution: fixed
Status: newclosed

In [17942]:

Fixed #15076 -- Quoted ForeignKey target class names in inspectdb when class is defined below.

Thanks saschwarz for the report, jeff@… for the initial patch and Ramiro Morales for the review.

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