Passing a list as parameter for IN statement using named arguments [duplicate]
Asked Answered
S

0

7

How can I pass a list to an IN statement in a query using psycopg's named arguments?

Example:

cur.execute("""
        SELECT name
        FROM users
        WHERE id IN (%(ids)s)
        """,
        {"ids": [1, 2, 3]})

When I do that, I get the following error message:

psycopg.errors.UndefinedFunction: operator does not exist: integer = smallint[]

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
Senghor answered 19/12, 2022 at 22:17 Comment(2)
... WHERE id = ANY(%(ids)s)""", {"ids": [1, 2, 3]} per List adaption.Holdup
This post is not a duplicate of the one that is linked, as that deals with psycopg2, and this one is about psycopg3, where that solution does not work for some reason.Cithara

© 2022 - 2025 — McMap. All rights reserved.