piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.45k stars 91 forks source link

`Array.any` bugged? #1126

Open Jacky56 opened 2 days ago

Jacky56 commented 2 days ago

I receive Query string compile errors when I use Array.any for postgres (cockroach) manager

from piccolo.table import Table
from piccolo.columns import (
    Array,
    Text,
)

class Helloworld(Table):
    test = Array(Text(), null=True, default=[])

Helloworld.objects().where(Helloworld.text.any("a hello world string")).run_sync()

result:

asyncpg.exceptions.IndeterminateDatatypeError: could not determine data type of placeholder $1

The work around is:

from piccolo.columns.combination import WhereRaw

Helloworld.objects().where(WhereRaw("some raw equivalent")).run_sync()
sinisaos commented 2 days ago

@Jacky56 Sorry but, I can't reproduce the error in Postgres. I noticed you have a typo in your where query (the column name is wrong, it should be Helloworld.test.any not Helloworld.text.any) but that raise AttributeError: object of type 'Helloworld' has no attribute 'text', but other than that everything works in my case. EDIT: You are right for CockroachDB. I can reproduce that error which advises adding explicit type conversions to placeholder arguments.

dantownsend commented 2 days ago

Yeah, it's weird that CockroachDB requires the explicit type casting.

You can add an explicit type cast using one of these:

from piccolo.query.functions import Cast
from piccolo.querystring import QueryString

# This
await MyTable.select().where(MyTable.my_array_column.any(Cast(QueryString('{}', 'some_value'), Varchar())))

# Or just this:
await MyTable.select().where(MyTable.my_array_column.any(QueryString('{}'::varchar, 'some_value'))