Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresql array in array #777

Open
kalra19 opened this issue Dec 5, 2023 · 3 comments
Open

Postgresql array in array #777

kalra19 opened this issue Dec 5, 2023 · 3 comments

Comments

@kalra19
Copy link

kalra19 commented Dec 5, 2023

Want to generate below query using pypika for postgresql.

select name, employee_id from employee where phone_no && ARRAY['7377','3877','9277']::varchar[]

How to create above query using pypika? Is there any way we can do that?

@wd60622
Copy link
Contributor

wd60622 commented Dec 6, 2023

So there is an Array object which when used with PostgreSQLQuery will give the ARRAY[...] syntax

from pypika import Field, Array
from pypika.dialects import PostgreSQLQuery

query = (
    PostgreSQLQuery
    .from_("employee").select("name", "employee_id")
    .where(Array("7377", "3877", "9277"))
)

which gets you some of the way

SELECT "name","employee_id" 
FROM "employee" 
WHERE ARRAY['7377','3877','9277']

I suspect the && will have to be built out like this class
and the ::varchar[] will have to be built off of the Term class

EDIT: Try out the ArithmeticExpression for the && and build out custom operation like in #779

@wd60622
Copy link
Contributor

wd60622 commented Jan 17, 2024

Hi @kalra19, were you able to find a solution or work around for this?

@kalra19
Copy link
Author

kalra19 commented Jan 17, 2024

No as of now, but for a work around solution, i have used sql string composition to generate the query.

FYR: https://www.psycopg.org/docs/sql.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants