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

Bug in sqlx.Named where ::text is incorrectly processed, leading to syntax erro #956

Open
KostyaDevelop opened this issue Nov 27, 2024 · 2 comments

Comments

@KostyaDevelop
Copy link

Description:

I encountered a bug while using sqlx.Named to bind query parameters in SQL queries that contain type casting (::text). The ::text syntax is being modified during the query parameter binding, causing the colon (:) to be removed. This leads to a syntax error in the SQL query.

Steps to Reproduce:
Use ::text in a SQL query for type casting in PostgreSQL.
Bind parameters using sqlx.Named.
The resulting query will have the colon (:) removed before text, causing a syntax error.

Example code:

companyId := 555
query := `SELECT DISTINCT
        t.path::text AS catalog_path
    WHERE t.company_id = :company_id
    FROM table AS t`
params := map[string]interface{}{
    "company_id": companyId,
}
query, args, err := sqlx.Named(query, params)

Resulting Query After sqlx.Named:

SELECT DISTINCT
        t.path:text AS catalog_path
    WHERE t.company_id = :company_id
    FROM table AS t

Error Encountered:
pq: syntax error at or near ":"

Expected Behavior:
The ::text type cast should remain unchanged in the query, and the parameter binding should work as expected.

Actual Behavior:
The colon (:) before text is removed, resulting in an invalid SQL query and a syntax error.

@javierbarboza
Copy link

can you change ::text for ::::text? this is working for me

@mskhviyu
Copy link

mskhviyu commented Jan 18, 2025

you can use cast(t.path as text) as catalog_path instead of t.path::text AS catalog_path

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

3 participants