Skip to content

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

Open

Description

@KostyaDevelop

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.

Activity

  1. javierbarboza commented on Dec 19, 2024

    @javierbarboza

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

  2. mskhviyu commented on Jan 18, 2025

    @mskhviyu

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

  3. rowanseymour commented on Oct 10, 2025

    @rowanseymour

    FYI this is fixed in https://github.com/vinovest/sqlx/ which uses a real parser and not regexes

  4. suiriass commented on Jun 18, 2026

    @suiriass

    Had the same problem. sqlex fixes this — drop-in replacement, works with ::text out of the box. https://github.com/go-sqlex/sqlex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions