Opened 7 years ago

Last modified 7 years ago

#28368 new Bug

Trying to create a model instance with an existing shared primary key in MTI silently updates an existing instance

Reported by: Lawrence Elitzer Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Lawrence Elitzer)

I am using model inheritance and when I set my own primary_key in the parent class as one of the fields I define, when I try to create child objects with the same primary key I am NOT getting a duplicate error. Instead, it is overwriting the parent class fields. In the two child tables in the example code below, both parent pointers in the two child object tables point to the same parent record.

# models.py
from django.db import models

class CommonImportedFile(models.Model):
    sha  = models.CharField(max_length=40, primary_key=True)
    name = models.CharField(max_length=200)

class FT1(CommonImportedFile):
    pass

class FT4(CommonImportedFile):
    pass

And here are the commands I am using:

from myapp.models import *
FT1.objects.create(name='ft1', sha='1234')
FT4.objects.create(name='ft4', sha='1234')
cf = CommonImportedFile.objects.get(sha='1234')
cf.name

cf.name is 'ft4' here indicating to me that the second object creation is overwriting the parent record entry since the primary_key 'sha' field is the same for both object creations. If you get rid of the primary_key attribute in the 'sha' field and instead put 'unique=True' then this works as I think it should. Seems like a bug to me, or am I using inheritance incorrectly here?

I am using Django 1.11.3 and Python 3.6.1 with MariaDB 5.5.52 on CentOS 7.

Change History (6)

comment:1 by Lawrence Elitzer, 7 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:2 by Lawrence Elitzer, 7 years ago

Description: modified (diff)

comment:3 by Lawrence Elitzer, 7 years ago

Description: modified (diff)

comment:4 by Tim Graham, 7 years ago

Resolution: invalid
Status: newclosed

Have you read the model inheritance documentation? You're using multi-table inheritance so FT1 and FT4 share a common parent table. Maybe you want to make CommonImportedFile an abstract model instead. If you understand those points and still think this is a bug, then please explain what the expected behavior would be. In the future, please use our support channels to ask "is it a bug?" questions. Thanks.

comment:5 by Lawrence Elitzer, 7 years ago

Resolution: invalid
Status: closednew

I have read through the Abstract Class before and since I want to access the parent model directly in my app, I need the multi-table inheritance. I expect the behavior to be that when creating objects that have the same parent primary_key there is a duplicate error reported instead of just overwriting the parent record. Does this make sense? I don't see why this isn't the case with multi-table inheritance. I can submit an email if that is better. I don't have access to any IRC clients at my workplace.

comment:6 by Tim Graham, 7 years ago

Summary: Model Inheritance Primary Key IssueTrying to create a model instance with an existing shared primary key in MTI silently updates an existing instance
Triage Stage: UnreviewedAccepted

I see your point. I'm not sure if that can or should be changed but if you offer a patch, we can evaluate it further.

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