Opened 11 years ago

Closed 10 years ago

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

Download all attachments as: .zip

Change History (9)

Changed 11 years ago by mir@…

Attachment: inspectdb-ordering.diff added

Changed 11 years ago by mir@…

Attachment: inspectdb-ordering.2.diff added

comment:1 Changed 11 years ago by mir@…

Removed first two lines which have confused trac

Changed 10 years ago by mir@…

Attachment: inspectdb-ordering.3.diff added

replaces other patches (major clean-up)

comment:2 Changed 10 years ago by mir@…

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 Changed 10 years ago by Malcolm Tredinnick

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

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 Changed 10 years ago by Adrian Holovaty

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

comment:6 Changed 10 years ago by mir@…

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