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:

  1. Create 2 models that have models.Model in bases and make migrations:
class A(models.Model):
    pass
class B(models.Model):
    pass
  1. 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:

  1. Rename ID field, change it to models.OneToOne with primary_key and make sure the user will prepopulate that field somehow.
  2. 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.

Change History (1)

in reply to:  description comment:1 by Mariusz Felisiak, 4 years ago

Resolution: worksforme
Status: newclosed

Replying to Vadim Fabrichnov:

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'.

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

btw, if you have more than one model in bases, you will get the error also, even if it's a freshly created model.

This is a documented behavior.

Note: See TracTickets for help on using tickets.
Back to Top