Opened 4 years ago
Closed 4 years ago
#32330 closed Bug (worksforme)
Inheritance for already existing models will throw an exception
Reported by: | Vadim Fabrichnov | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Steps to reproduce:
- Create 2 models that have models.Model in bases and make migrations:
class A(models.Model): pass class B(models.Model): pass
- Try to apply multi-table inheritance to one of them:
class B(A): pass
Results:
You will get an exception:
django.core.exceptions.FieldError: Local field 'id' in class 'B' clashes with field of the same name from base class 'A'.
btw, if you have more than one model in bases, you will get the error also, even if it's a freshly created model.
I can see 2 possible solutions:
- Rename ID field, change it to models.OneToOne with primary_key and make sure the user will prepopulate that field somehow.
- Just raise the exception and ask the user to write migration manually.
This ticket is kinda related to https://code.djangoproject.com/ticket/23521 that I already fixed. If it's worth working on, I can fix this ticket also.
Note:
See TracTickets
for help on using tickets.
Replying to Vadim Fabrichnov:
I cannot reproduce this issue, after changing inheritance
makemigrations
creates (as expected) two operations:Remove field id from b
Add field a_ptr to b
This is a documented behavior.