#20098 closed New feature (fixed)
Add validation for models with the same db_table
Reported by: | Owned by: | Sanyam Khurana | |
---|---|---|---|
Component: | Core (System checks) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | bmispelon@… | 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
When declaring two models using a custom db_table setting in the Meta class, I found out that django failed to detect that the two models declared the same db_table and subsequently it will fail on syncdb when trying to create the same table twice.
This, of course, was introduced by copy&paste, but at least django should report this on validate instead of failing when trying to syncdb.
Attachments (5)
Change History (21)
comment:1 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Bug → Cleanup/optimization |
by , 12 years ago
Attachment: | patch20098 added |
---|
comment:2 by , 12 years ago
Cc: | added |
---|---|
Has patch: | set |
Needs tests: | set |
This seems like a good idea but it needs to have tests in order for it to be merged.
From what I can tell, the tests for the validate
command go in tests/admin_scripts/tests.py
: https://github.com/django/django/blob/master/tests/admin_scripts/tests.py#L1046
Also, consider adding a .diff
extension to your patch so that trac can use syntax highlighting on it.
Thanks.
follow-up: 10 comment:3 by , 12 years ago
It is worth bearing in mind that there are valid use cases for encountering duplicate table names, such as when using proxy models, or unmanaged models. The provided patch doesn't seem to account for that, but I'm not familiar enough with the surrounding code to know whether it is already handled.
follow-up: 6 comment:4 by , 12 years ago
Thanks for pointing this out. I included a revised version of the patch including also two test cases, one that tests against a single application declaring duplicate db tables and one that tests against multiple applications declaring duplicate db tables.
by , 12 years ago
Attachment: | 20098.patch added |
---|
comment:5 by , 12 years ago
As proxies reuse the actual model the newly provided patch should be sufficient.
comment:6 by , 12 years ago
Replying to carsten.klein@…:
Thanks for pointing this out. I included a revised version of the patch including also two test cases, one that tests against a single application declaring duplicate db tables and one that tests against multiple applications declaring duplicate db tables.
As far as unmanaged models go, these should not be using the ModelBase meta class then, or would they?
comment:7 by , 12 years ago
There's a typo in the added comment:
Lookup table for table names introduced in order to prevent that users from declaring the same table twice
Regarding unmanaged and proxy models, I'm not familiar enough with the code to tell if they are handled with this patch but one surefire way to tell would be to add some tests that use them.
by , 12 years ago
Attachment: | 20098-support.patch added |
---|
comment:9 by , 12 years ago
As a side effect this should also detect copy&paste errors such as duplicating a model class and failing to rename it and updating its Meta accordingly.
comment:10 by , 12 years ago
Replying to Keryn Knight <django@…>:
It is worth bearing in mind that there are valid use cases for encountering duplicate table names, such as when using proxy models, or unmanaged models. The provided patch doesn't seem to account for that, but I'm not familiar enough with the surrounding code to know whether it is already handled.
Just had a look at the documentation on unmanaged models. It seems that this is a special case that needs to be dealt with. I will add a guard to the patch and a test case that also includes unmanaged models.
Thanks for pointing this out!
by , 12 years ago
Attachment: | 20098.2.patch added |
---|
Correct handling of unmanaged models and duplicate db_table declaration
comment:11 by , 9 years ago
Component: | Database layer (models, ORM) → Core (System checks) |
---|---|
Summary: | Django validate command fails to detect that multiple models declare the same db_table → Add validation for models with the same db_table |
Type: | Cleanup/optimization → New feature |
Version: | 1.5 → master |
comment:12 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
There is a lot of regression in applying the patch cleanly which revolve mostly around the test cases within Django. I'm working to modify the last version of the patch, cleanly apply it to the master branch and fix test cases.
PR will be up soon.
comment:13 by , 6 years ago
Needs tests: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Not sure if it's worth adding a co-author given the final patch is quite different from the original one here.
comment:14 by , 6 years ago
Hello Tim,
I've addressed your reviews on the patch. Can you please have a look at this when you've some time?
Please let me know if there are any more changes needed.
Thanks!
Proposed patch to django.db.models.options.py that might resolve the issue (tested)