RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.85k stars 81 forks source link

Prisma errors after being called with empty list #972

Open sleepylemur opened 3 months ago

sleepylemur commented 3 months ago

Bug description

When passing an empty list to a query that coerces it to timestamps, a subsequent call with a non empty list causes the db to throw an exception.

Throws ERROR: improper binary format in array element 1:

await client.query_raw("select $1::timestamp[]", [])
await client.query_raw("select $1::timestamp[]", ["2024-06-17T21:34:42"])

If the list is always non-empty or the list is always empty, everything works fine.

Works fine:

await client.query_raw("select $1::timestamp[]", [])
await client.query_raw("select $1::timestamp[]", [])

Also works fine:

await client.query_raw("select $1::timestamp[]", ["2024-05-17T21:34:42"])
await client.query_raw("select $1::timestamp[]", ["2024-06-17T21:34:42"])

This wasn't terribly hard to work around once we understood what was happening, but took a bit of effort to debug because of the surprising behavior.

How to reproduce

Run example.py with a postgresql database:

DATABASE_URL="postgresql://$USER@0.0.0.0:5432/postgres" python -m example

example.py

import asyncio
from prisma import Prisma

async def main():
    client = Prisma()
    await client.connect()
    print(await client.query_raw("select $1::timestamp[]", []))
    print(await client.query_raw("select $1::timestamp[]", ["2024-06-17T21:34:42"]))
    await client.disconnect()

asyncio.run(main())

Expected behavior

Prisma/PostgreSQL throws

prisma.errors.RawQueryError: ERROR: improper binary format in array element 1

Prisma information

Not relevant

Environment & setup

prisma                : 5.11.0
@prisma/client        : Not found
Computed binaryTarget : darwin-arm64
Operating System      : darwin
Architecture          : arm64
Node.js               : v20.10.0
Query Engine (Binary) : query-engine efd2449663b3d73d637ea1fd226bafbcf45b3102 (at ../../../.cache/prisma-python/binaries/5.11.0/efd2449663b3d73d637ea1fd226bafbcf45b3102/node_modules/@prisma/engines/query-engine-darwin-arm64)
Schema Engine         : schema-engine-cli efd2449663b3d73d637ea1fd226bafbcf45b3102 (at ../../../.cache/prisma-python/binaries/5.11.0/efd2449663b3d73d637ea1fd226bafbcf45b3102/node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm           : @prisma/prisma-schema-wasm 5.11.0-15.efd2449663b3d73d637ea1fd226bafbcf45b3102
Default Engines Hash  : efd2449663b3d73d637ea1fd226bafbcf45b3102
Studio                : 0.499.0
RobertCraigie commented 2 months ago

Thanks for the detailed bug report, I can reproduce the error. I've also verified that this doesn't happen in the JS client.

sleepylemur commented 2 months ago

Thanks for investigating. Again, low priority on our end due to there being an easy workaround.