Foreign Key Query Error In Case Of Custom Uint64 Field Which Was Used As A Primary Key

- 1 answer

I needed uint64 field as primary key field for my database, expectedly other models used foreign key to point to this model. Resulting code was something like this:

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')

This code works properly for all requests used by me expect one:

>>> f = Foo.create(id=10000)
>>> Bar.create(id=123, foo=f)
<ptest.Bar object at 0x7f2fde7f7e10>
>>> b = Bar.get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/", line 1151, in __get__
    return self.get_object_or_id(instance)
  File "/usr/local/lib/python3.4/dist-packages/", 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/", line 4403, in get
    return sq.get()
  File "/usr/local/lib/python3.4/dist-packages/", line 2812, in get
    return clone.execute().next()
  File "/usr/local/lib/python3.4/dist-packages/", line 2859, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/usr/local/lib/python3.4/dist-packages/", line 2555, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/usr/local/lib/python3.4/dist-packages/", line 3359, in execute_sql
    cursor.execute(sql, params or ())
OverflowError: Python int too large to convert to SQLite INTEGER

It seems that somewhere conversion between python value and db value was not done properly. Looks like a bug to me.

Any ideas how it can be fixed without directly saving foo id in Bar as UInt64 instead of use of ForeignKeyField?



It was a bug in peewee, which is now fixed: