Opened 14 years ago

Closed 14 years ago

Last modified 13 years ago

#12560 closed (fixed)

Unique validation should not check for null atuo-assigned PK

Reported by: Karen Tracey Owned by: nobody
Component: Uncategorized Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Initially noted here: http://groups.google.com/group/django-developers/browse_thread/thread/b45c4d599ea93b74#

The model validation unique check code is issuing an unnecessary query when adding new instances of models with auto-assigned PKs:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> django.get_version()
u'1.2 alpha 1 SVN-12120'
>>> from ttt.models import Tag
>>> from django import forms
>>> class TagForm(forms.ModelForm):
...     class Meta:
...         model = Tag
... 
>>> tf1 = TagForm({'name': 'T1'})
>>> tf1.is_valid()
True
>>> from django.db import connection
>>> connection.queries
[{'time': '0.002', 'sql': u'SELECT (1) AS `a`, `ttt_tag`.`id`, `ttt_tag`.`name` FROM `ttt_tag` WHERE `ttt_tag`.`id` IS NULL LIMIT 1'}]
>>> 

Besides being unnecessary, that's a query that will potentially return an unexpected result on MySQL, see: http://groups.google.com/group/django-developers/browse_thread/thread/d4180b8addf5e970/

This is similar to #12132, only for adding new instances instead of changing existing ones.

Attachments (1)

12560.diff (1.8 KB ) - added by Honza Král 14 years ago.
Quick fix to get the discussion started, may not be 100%

Download all attachments as: .zip

Change History (5)

by Honza Král, 14 years ago

Attachment: 12560.diff added

Quick fix to get the discussion started, may not be 100%

comment:1 by Honza Král, 14 years ago

Has patch: set

Added a quick patch, haven't checked everything, so only take it as an idea how to solve this. Unfortunately I don't have time this weekend, but I am hoping the sprinters might.

comment:2 by Waldemar Kornewald, 14 years ago

Any progress? This is just a one-line fix. Maybe it should be limited to primary_key fields, I don't know.

comment:3 by jkocherhans, 14 years ago

Resolution: fixed
Status: newclosed

(In [12227]) Fixed #12560. Changed validate_unique to stop checking null primary key values. Thanks, Honza Král.

comment:4 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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