tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Blob field (BinaryField) may not works

Open fy0 opened this issue 5 years ago • 7 comments

Describe the bug Can't query by blob fields, can't update blob values.

To Reproduce

from tortoise import Tortoise, fields, run_async
from tortoise.models import Model


class Event(Model):
    id = fields.IntField(pk=True)
    bin = fields.BinaryField()

    class Meta:
        table = "event"

    def __str__(self):
        return self.name


async def run():
    await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
    await Tortoise.generate_schemas()

    event = await Event.create(bin=b'aabb')    
    print(Event.filter(bin=b"aabb").sql())

    print(await Event.filter(id=1).update(bin=b'aacc'))


if __name__ == "__main__":
    run_async(run())

Expected behavior Code just works. Actually cause error.

sql generated: SELECT "id","bin" FROM "event" WHERE "bin"=b'aabb'

Additional context

fy0 avatar Nov 02 '20 08:11 fy0

docs said it. https://tortoise-orm.readthedocs.io/en/latest/fields.html#module-tortoise.fields.data

long2ice avatar Nov 02 '20 08:11 long2ice

@long2ice Does tortoise-orm have any plan to support it?

fy0 avatar Nov 02 '20 08:11 fy0

After a little time search, maybe filter for blob is unnecessary

long2ice avatar Nov 02 '20 08:11 long2ice

After a little time search, maybe filter for blob is unnecessary

No, it's common situation.

For example, when use a distributed id algorithm like snowflake, uuid and so on, blob field is natural choice.

class User:
    id = BlobField(primary_key=True)
    name = CharField()


class Topic:
    title = CharField()
    content = TextField()
    author = BlobField()

fy0 avatar Nov 02 '20 09:11 fy0

Why not use UUIDField or CharField directly?

long2ice avatar Nov 02 '20 09:11 long2ice

Why not use UUIDField or CharField directly?

CharField needs 2x space, and this will cause additional serialization and deserialization.

blob is part of sql standard, if native type works, why not use it?

fy0 avatar Nov 02 '20 09:11 fy0

still can not filter the blob

purplegrapeZz avatar May 03 '22 03:05 purplegrapeZz