2

在我的 django 项目中,我正在使用旧数据库,它有一个包含多个主键的表。save()当我在某些模型实例上调用方法时,我面临着 Django ORM 错误地构造 SQL 查询的问题。

例如,我有以下模型:

class MyTable(models.Model):
    field_1 = models.IntegerField(primary_key=True)
    field_2 = models.BooleanField(default=False, null=False)
    field_3 = models.BooleanField(default=False, null=False)
    user = models.ForeignKey(
        CustomUser, models.DO_NOTHING, db_column='userid', primary_key=True)

    class Meta:
        managed = False
        db_table = 'my_table'
        unique_together = (('user', 'field_1'),)

我得到这样的模型实例:mytable_instance = MyTable.objects.all()[0]. 然后我想修改那个实例:

mytable_instance.field_2 = True
mytable_instance.field_2.save()

然而,Django ORM 执行这个查询:

{'sql': 'UPDATE `my_table` SET `field_2` = 1, `field_3` = 0 WHERE `my_table`.`field_1` = 123', 'time': '0.000'}

field_2这是不正确的,因为它将field_3为所有行my_table设置新值field_1 = 123

我怎样才能克服这个问题?我需要这样的 SQL 查询:

'UPDATE `my_table` SET `field_2` = 1, `field_3` = 0 WHERE `my_table`.`field_1` = 123 AND `my_table`.`user_id` = 1'

编辑:完整代码

遗留数据库表的模型:

class MyTable(models.Model):
    field1_id = models.IntegerField(db_column='field1id', primary_key=True)
    is_favorite = models.BooleanField(db_column='isfavorite', default=False, null=False)
    is_admin = models.BooleanField(db_column='isadmin', default=False, null=False)
    role = models.IntegerField(default=USER_GUEST, null=False)
    field2 = models.BooleanField(null=False, default=True)
    field3 = models.BooleanField(null=False, default=True)
    is_active = models.BooleanField(db_column='isactive', null=False, default=True)

    user = models.ForeignKey(
        CustomUser, models.DO_NOTHING, db_column='userid', primary_key=True)

    DB_NAME = 'default'

    class Meta:
        managed = False
        db_table = 'mytable'
        unique_together = (('user', 'field1_id'),)

查询:

>>> from web_services.apps.my_app.models import MyTable
>>> g = MyTable.objects.get(field1_id=12)
>>> g.is_active = True
>>> g.save()
>>> connection.queries[-1]
{'time': '0.000', 'sql': 'UPDATE `mytable` SET `isfavorite` = 0, `isadmin` = 1, `role` = 3, `field2` = 1, `field3` = 1, `isactive` = 1 WHERE `mytable`.`field1id` = 12'}

但是我需要:

{'time': '0.000', 'sql': 'UPDATE `mytable` SET `isfavorite` = 0, `isadmin` = 1, `role` = 3, `field2` = 1, `field3` = 1, `isactive` = 1 WHERE `mytable`.`field1id` = 12' AND `mytable`.`userid` = 1'}
4

1 回答 1

2

让我们看看这两个查询:

UPDATE `mytable` 
SET `isfavorite` = 0, `isadmin` = 1, `role` = 3, `field2` = 1, `field3` = 1, `isactive` = 1 
WHERE `mytable`.`field1id` = 12

UPDATE `mytable` 
SET `isfavorite` = 0,`isadmin` = 1, `role` = 3, `field2` = 1, `field3` = 1, `isactive` = 1 
WHERE `mytable`.`field1id` = 12' AND `mytable`.`userid` = 1

两者之间的唯一区别是AND userid=1位。

你这么说确实是对的

它将为 my_table 中 field1_id = 12 的所有行的 field_2 和 field_3 设置新值

恰好有这样的一行。为什么?因为您已将 field_id 定义为主键。这保证只有一行 field1_id = N ,其中 N 是任何整数。

所以 django 查询表现完美。

于 2016-05-13T12:20:40.720 回答