Strange SQL Query

0 views
Skip to first unread message

Maksymus007

unread,
Nov 4, 2009, 3:20:21 AM11/4/09
to django...@googlegroups.com
I'm trying to use modelformset for my model using specified form.
Everything works well unless i try to save forms - ale changes are
saved, but for 22 forms django generates 91 queries (some of them are
for auth, other parts etc - but this number should not exceed 30
counting one query for each form). Nevertheless, i got 22 queries like

SELECT (1) AS "a" FROM "finances"."gates_approvals" WHERE
("finances"."gates_approvals"."gate_approval_id" = 45 AND NOT
("finances"."gates_approvals"."gate_approval_id" = 45 ))

which is totally useless. Did anyone meet such a strange behaviour?

Mark L.

unread,
Nov 4, 2009, 5:13:52 AM11/4/09
to Django users
Hello

Yes, I've seen this behaviour, all right. This looks like a
misbehaving uniqueness check and I've seen it happen in the .clean()
method of the model (in model-validation branch), and, while this is
certainly not where *you* are seeing it, the queries look just the
same.

This is the way Django handles uniqueness checks (and, quite frankly,
this is the only way to do it), however it should not be performed for
primary keys. If it wasn't a primary key field but some other
"other_unique_field" the query would look like this 'select (1) as "a"
from "table" where ("table"."other_unique_field" = 45 and not
("table"."id" = 45))', which makes perfect sense to me (this
observation was made by another django-users member, so I am not the
one to be thanked here).

Anyway, I haven't (yet) read the source of ModelFormSet, so I can't
give you any direct advice as to how to get rid of these queries.
Could you post your model declaration, by the way?

I hope my explanation helped at least a little bit.

Mark

Maksymus007

unread,
Nov 4, 2009, 5:26:00 AM11/4/09
to django...@googlegroups.com

Sure:

class GatesApprovals(models.Model):
id = models.AutoField(primary_key=True, null=False,
db_column='gate_approval_id')
gate = models.ForeignKey(Gates, null=False, db_column='gate_approval_gate')
period = models.DateField(null=False, db_column='gate_approval_period')
value = models.DecimalField(null=True, default=None, max_digits=5,
decimal_places=2, db_column='gate_approval_value')

def __unicode__(self):
return str(self.gate) + ' / ' + str(self.period)

class Meta:
db_table = 'finances"."gates_approvals'
managed = False

and Gates:

class Gates(models.Model):
gate = models.IntegerField(primary_key=True,db_column='gate_id')
in_value = models.DecimalField(default=0,max_digits=4,decimal_places=2,db_column='gate_in_value')
out_value =
models.DecimalField(default=0,max_digits=4,decimal_places=2,db_column='gate_out_value')
out_limit = models.SmallIntegerField(default=0,db_column='gate_out_limit')

shared = models.BooleanField(null=False, default=False,
db_column='gate_shared')

class Meta:
db_table = u'data"."gates'
managed = False

def __unicode__(self):
return str(self.gate)

there is nothing like unique together or something - all those are
handled on database level.

I use modelformset_factory with custom form. First it was pure
ModelForm with meta part (maybe doubling instance managing i thought,
but that was not the case), then ModelForm without meta info - with
the same result. Pure form cannot be used with modelformset_factory.

Reply all
Reply to author
Forward
0 new messages