Code

Opened 3 years ago

Closed 2 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 3 years ago.
django-fk-quoting.diff (1.3 KB) - added by jeff@… 3 years ago.
Quote ForeignKey models only if the model has not appeared yet.
15194-3.diff (2.3 KB) - added by claudep 2 years ago.
Identical patch + updated test

Download all attachments as: .zip

Change History (15)

Changed 3 years ago by saschwarz

comment:1 Changed 3 years ago by ramiro

  • Component changed from Uncategorized to ORM aggregation
  • Needs documentation unset
  • Needs tests unset
  • Owner nobody deleted
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design 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 3 years ago by ramiro (next)

comment:2 Changed 3 years ago by ramiro

  • Component changed from ORM aggregation to django-admin.py inspectdb
  • Owner set to nobody

Changed 3 years ago by jeff@…

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

comment:3 Changed 3 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 3 years ago by gabrielhurley

  • Component changed from django-admin.py inspectdb to Core (Management commands)

comment:5 Changed 3 years ago by jaddison

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 3 years ago by aaugustin

  • Easy pickings unset
  • UI/UX unset

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

comment:7 Changed 3 years ago by ramiro

  • Keywords inspectdb added

comment:8 Changed 2 years ago by ramiro

  • Needs tests set

comment:9 Changed 2 years ago by ramiro

  • Triage Stage changed from Design decision needed to Accepted

#9164 was a duplicate.

Changed 2 years ago by claudep

Identical patch + updated test

comment:10 Changed 2 years ago by claudep

  • Has patch set
  • Needs tests unset

comment:11 Changed 2 years ago by ramiro

  • Triage Stage changed from Accepted to Ready for checkin

comment:12 Changed 2 years ago by claudep

  • Resolution set to fixed
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.