Code

Opened 5 years ago

Closed 5 years ago

#12132 closed (fixed)

possibly unneccessary unique_check for primary key fields is models

Reported by: Mark Owned by: nobody
Component: Database layer (models, ORM) Version: soc2009/model-validation
Severity: Keywords: model-validation validation unique
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation:
Needs tests: Patch needs improvement:
Easy pickings: UI/UX:

Description

Every time a clean() method of the model instance is run, validate_unique() is performed. It seems, that _get_unique_checks doesn't get into account, that the primary key doesn't need such a check. The sql that is run during the check is meaningless (it is only meaningless if we are checking a primary key field):

SELECT (1) AS "a" FROM "test_models_testmodel" WHERE ("test_models_testmodel"."id" = 1  AND NOT ("test_models_testmodel"."id" = 1 ))

This results in unnecessary database hits and (possible) performance degradation. I have put together a simple test rack, so that the effect can be easily observed. It's not a real test case, but I really hope, that it will be sufficient.

If I understand correctly, it can be easily fixed, by checking if a field is a primary key inside the _get_unique_checks model instance method (patch included).

Attachments (2)

test_validation.tar.gz (554 bytes) - added by Mark 5 years ago.
A runnable test and an app directory
patch.diff (588 bytes) - added by Mark 5 years ago.
Proposed fix

Download all attachments as: .zip

Change History (3)

Changed 5 years ago by Mark

A runnable test and an app directory

Changed 5 years ago by Mark

Proposed fix

comment:1 Changed 5 years ago by Honza_Kral

  • Resolution set to fixed
  • Status changed from new to closed

(In [11856]) [soc2009/model-validation] Fixed #12132: unneccessary unique_check for primary key when not adding model

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.