#1561 closed enhancement (wontfix)
[patch] enhance inspectdb to reorder models and get a functional model immediately
Reported by: | 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)
Change History (9)
by , 19 years ago
Attachment: | inspectdb-ordering.diff added |
---|
by , 19 years ago
Attachment: | inspectdb-ordering.2.diff added |
---|
comment:1 by , 19 years ago
by , 19 years ago
Attachment: | inspectdb-ordering.3.diff added |
---|
replaces other patches (major clean-up)
comment:2 by , 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 , 18 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 , 18 years ago
Cc: | added; removed |
---|---|
Keywords: | rthml tab space editor js added; inspectdb removed |
Summary: | [patch] enhance inspectdb to reorder models and get a functional model immediately → hi-world cup |
comment:5 by , 18 years ago
Summary: | hi-world cup → [patch] enhance inspectdb to reorder models and get a functional model immediately |
---|
comment:6 by , 18 years ago
Cc: | removed |
---|---|
Keywords: | rthml tab space editor js removed |
Resolution: | → wontfix |
Status: | new → closed |
I think forward references are the way to go, and the patch doesn't apply any more, anyway. --> Closing
Removed first two lines which have confused trac