Opened 19 years ago

Closed 18 years ago

Last modified 18 years ago

#1561 closed enhancement (wontfix)

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

Reported by: mir@… Owned by: Adrian Holovaty
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: no UI/UX: no

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@… 19 years ago.
inspectdb-ordering.2.diff (9.5 KB ) - added by mir@… 19 years ago.
inspectdb-ordering.3.diff (6.9 KB ) - added by mir@… 19 years ago.
replaces other patches (major clean-up)

Download all attachments as: .zip

Change History (9)

by mir@…, 19 years ago

Attachment: inspectdb-ordering.diff added

by mir@…, 19 years ago

Attachment: inspectdb-ordering.2.diff added

comment:1 by mir@…, 19 years ago

Removed first two lines which have confused trac

by mir@…, 19 years ago

Attachment: inspectdb-ordering.3.diff added

replaces other patches (major clean-up)

comment:2 by mir@…, 19 years ago

Summary: [patch] enhance inspectdb to get a functional model immediately[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 by Malcolm Tredinnick, 19 years ago

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 by hi-world cup, 18 years ago

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

comment:5 by Adrian Holovaty, 18 years ago

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

comment:6 by mir@…, 18 years ago

Cc: hi-world cup removed
Keywords: rthml tab space editor js removed
Resolution: wontfix
Status: newclosed

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