tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.66k stars 387 forks source link

Blob field (BinaryField) may not works #539

Open fy0 opened 4 years ago

fy0 commented 4 years ago

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

long2ice commented 4 years ago

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

fy0 commented 4 years ago

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

long2ice commented 4 years ago

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

fy0 commented 4 years ago

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()
long2ice commented 4 years ago

Why not use UUIDField or CharField directly?

fy0 commented 4 years ago

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?

purplegrapeZz commented 2 years ago

still can not filter the blob