Django设置两个主键后get查询修改报错的bug记录

场景还原

环境
  • Mysql 5.7
  • Python 3.7
  • Django 2.1
  • 数据库表结构
    在这里插入图片描述
  • 数据表数据
    在这里插入图片描述
  • 反向生成后的models
class TestTable(models.Model):
    key1 = models.CharField(primary_key=True, max_length=10)
    key2 = models.CharField(max_length=10)
    value = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'test_table'
        unique_together = (('key1', 'key2'),)
  • 测试函数
def bug_test(request):
    obj=models.TestTable.objects.get(key1='1',key2='3')
    print(obj.value)
    obj.value='6'
    obj.save()
    return HttpResponse('true')

测试结果

控制台报错 django.db.utils.IntegrityError: (1062, “Duplicate entry ‘1-3’ for key ‘PRIMARY’”)

5
Internal Server Error: /bug_test/
Traceback (most recent call last):
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\cursors.py", line 170, in execute
    result = self._query(query)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\cursors.py", line 328, in _query
    conn.query(q)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result
    result.read()
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 683, in _read_packet
    packet.check_error()
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1062, "Duplicate entry '1-3' for key 'PRIMARY'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\Git\testdjango\td\views.py", line 41, in bug_test
    obj.save()
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\models\base.py", line 718, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\models\base.py", line 748, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\models\base.py", line 812, in _save_table
    forced_update)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\models\base.py", line 861, in _do_update
    return filtered._update(values) > 0
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\models\query.py", line 712, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1383, in execute_sql
    cursor = super().execute_sql(result_type)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1065, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\cursors.py", line 170, in execute
    result = self._query(query)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\cursors.py", line 328, in _query
    conn.query(q)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result
    result.read()
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\connections.py", line 683, in _read_packet
    packet.check_error()
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\shanghaobo\Anaconda3\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
django.db.utils.IntegrityError: (1062, "Duplicate entry '1-3' for key 'PRIMARY'")
[26/Oct/2019 09:51:39] "GET /bug_test/ HTTP/1.1" 500 206620

原因分析
  • 暂不清楚原因
  • 解决方法:用filter查询 update更新
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页