Opened 8 years ago

Last modified 8 years ago

#28368 new Bug

Model Inheritance Primary Key Issue — at Version 2

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
Pull Requests:How to create a pull request

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 v. 1.11.3 and Python 3.6.1 with MariaDB 5.5.52 on CentOS 7.

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (2)

comment:1 by Lawrence Elitzer, 8 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:2 by Lawrence Elitzer, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top