Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#1561 closed enhancement (wontfix)

[patch] enhance inspectdb to reorder models and get a functional model immediately

Reported by: mir@… Owned by: adrian
Component: Database layer (models, ORM) Version: magic-removal
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

inspectdb should order Models so that you can really use the result:

  • avoid forward dependencies
  • use unique related_name for multiple references to the same class

The attached patch attempts a smart sort, and figures out when a related_name is required. It also tries to handle circular foreign key references in a sensible way, i.e. adds comments about the problem and replaces the ForeignKey with a plain field.

Attachments (3)

inspectdb-ordering.diff (9.6 KB) - added by mir@… 9 years ago.
inspectdb-ordering.2.diff (9.5 KB) - added by mir@… 9 years ago.
inspectdb-ordering.3.diff (6.9 KB) - added by mir@… 9 years ago.
replaces other patches (major clean-up)

Download all attachments as: .zip

Change History (9)

Changed 9 years ago by mir@…

Changed 9 years ago by mir@…

comment:1 Changed 9 years ago by mir@…

Removed first two lines which have confused trac

Changed 9 years ago by mir@…

replaces other patches (major clean-up)

comment:2 Changed 9 years ago by mir@…

  • Summary changed from [patch] enhance inspectdb to get a functional model immediately to [patch] enhance inspectdb to reorder models and get a functional model immediately

Reorganized code - I must admit the old patch was a mess. Now it should be much more obvious what's happening.

It only touches stuff for inspectdb, so there's no danger that it breaks a working application. I've tested it with mysql 4.1 and a rather complicated database. There's only one drawback, generating models is noticeable slower with this patch. With big models, a lot.

A short summary of what the patch does:

  • it adds an inner function into inspectdb, topological_sort. This function analyzes the foreign key dependencies between the tables and generates the most appropriate order of table names (and some additional information about forward references). It tries hard to handle circular dependencies in an smart way.
  • inspectdb is modified to use this function for ordering, treat forward references specially (use IntegerField instead of ForeignKey and add comments).
  • the generated comment that you need to reorder Models has been removed

comment:3 Changed 9 years ago by mtredinnick

Forward references can, in theory (it may contain bugs still), be handled by passing the name of the class as a string to the ForeignKey constructor. This only works if the class is in the same file, which will be the case here. So if we want to add this patch, the comments and forward reference stuff can be replaced with proper ForeignKeys.

comment:4 Changed 9 years ago by hi-world cup

  • Cc hi-world cup added; mir@… removed
  • Keywords rthml tab space editor js added; inspectdb removed
  • Summary changed from [patch] enhance inspectdb to reorder models and get a functional model immediately to hi-world cup

comment:5 Changed 9 years ago by adrian

  • Summary changed from hi-world cup to [patch] enhance inspectdb to reorder models and get a functional model immediately

comment:6 Changed 9 years ago by mir@…

  • Cc hi-world cup removed
  • Keywords rthml tab space editor js removed
  • Resolution set to wontfix
  • Status changed from new to closed

I think forward references are the way to go, and the patch doesn't apply any more, anyway. --> Closing

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