用作主键的自定义 uint64 字段的情况下的外键查询错误
Foreign key query error in case of custom uint64 field which was used as a primary key
我需要 uint64 字段作为我的数据库的主键字段,预计其他模型使用外键指向该模型。结果代码是这样的:
from peewee import *
db = SqliteDatabase('test.db')
class UInt64(Field):
db_field = 'bigint'
def db_value(self, value):
return value - (1<<63)
def python_value(self, value):
return value + (1<<63)
class Foo(Model):
id = UInt64(primary_key = True)
class Meta:
database = db
class Bar(Model):
id = UInt64(primary_key = True)
foo = ForeignKeyField(Foo, related_name='bars')
此代码适用于我使用的所有请求,除了一个:
>>> f = Foo.create(id=10000)
>>> Bar.create(id=123, foo=f)
<ptest.Bar object at 0x7f2fde7f7e10>
>>> b = Bar.get()
>>> b.id
123
>>> b.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 1151, in __get__
return self.get_object_or_id(instance)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 1142, in get_object_or_id
obj = self.rel_model.get(self.field.to_field == rel_id)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 4403, in get
return sq.get()
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 2812, in get
return clone.execute().next()
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 2859, in execute
self._qr = ResultWrapper(model_class, self._execute(), query_meta)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 2555, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 3359, in execute_sql
cursor.execute(sql, params or ())
OverflowError: Python int too large to convert to SQLite INTEGER
似乎 python 值和 db 值之间的某处转换没有正确完成。在我看来像是一个错误。
知道如何解决这个问题而不直接将 Bar 中的 foo id 保存为 UInt64 而不是使用 ForeignKeyField 吗?
这看起来像是您正在使用的 Python sqlite 驱动程序中的错误。它正在尝试转换为 int 并溢出。
这是 peewee 中的一个错误,现已修复:
https://github.com/coleifer/peewee/issues/791
我需要 uint64 字段作为我的数据库的主键字段,预计其他模型使用外键指向该模型。结果代码是这样的:
from peewee import *
db = SqliteDatabase('test.db')
class UInt64(Field):
db_field = 'bigint'
def db_value(self, value):
return value - (1<<63)
def python_value(self, value):
return value + (1<<63)
class Foo(Model):
id = UInt64(primary_key = True)
class Meta:
database = db
class Bar(Model):
id = UInt64(primary_key = True)
foo = ForeignKeyField(Foo, related_name='bars')
此代码适用于我使用的所有请求,除了一个:
>>> f = Foo.create(id=10000)
>>> Bar.create(id=123, foo=f)
<ptest.Bar object at 0x7f2fde7f7e10>
>>> b = Bar.get()
>>> b.id
123
>>> b.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 1151, in __get__
return self.get_object_or_id(instance)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 1142, in get_object_or_id
obj = self.rel_model.get(self.field.to_field == rel_id)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 4403, in get
return sq.get()
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 2812, in get
return clone.execute().next()
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 2859, in execute
self._qr = ResultWrapper(model_class, self._execute(), query_meta)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 2555, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 3359, in execute_sql
cursor.execute(sql, params or ())
OverflowError: Python int too large to convert to SQLite INTEGER
似乎 python 值和 db 值之间的某处转换没有正确完成。在我看来像是一个错误。
知道如何解决这个问题而不直接将 Bar 中的 foo id 保存为 UInt64 而不是使用 ForeignKeyField 吗?
这看起来像是您正在使用的 Python sqlite 驱动程序中的错误。它正在尝试转换为 int 并溢出。
这是 peewee 中的一个错误,现已修复: https://github.com/coleifer/peewee/issues/791