Code

Opened 6 years ago

Closed 6 years ago

#8463 closed (invalid)

force_insert doesn't work with inherited models

Reported by: nidi Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The following testcase describes the problem best:

Model:

from django.db import models

class Parent(models.Model):
    something = models.SlugField()

class Daughter(Parent):
    pass

Interactive session:

In [1]: from appname.models import Daughter
In [2]: d = Daughter(something='algo')
In [3]: d.save()
In [4]: d.something='otra'
In [5]: d.save(force_insert=True)

results in

IntegrityError: duplicate key value violates unique constraint "appname_daughter_pkey"

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by mtredinnick

  • milestone 1.0 deleted
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

That's not a bug, it's exactly the right behaviour. You created d and then you try to save it with updated data (the primary key will still be the same), but say you want to force an insert. The database says the record already exists with that primary key value.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.