Skip to content

[BUG] str.replace is not a function with 2 values CTEs #1149

@TanguyP

Description

@TanguyP

I have the following (much simplified) code:

const properties = { latitude: '3.1', longitude: '15.6' };
return await sql`
    WITH
        coords(lat, lon) AS (VALUES
            (${properties.latitude}, ${properties.longitude})
        )
        ,props(name) as (VALUES ${sql(Object.keys(properties).map(key => [key]))})
    SELECT '123'
`;

It fails with the following error:

TypeError: str.replace is not a function
    at escape (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:217:20)
    at file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:213:22
    at Array.map (<anonymous>)
    at escapeIdentifiers (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:213:13)
    at Object.select [as fn] (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:139:12)
    at Builder.build (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:71:17)
    at stringifyValue (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:109:38)
    at stringify (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:100:16)
    at build (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:228:20)
    at execute (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:172:7)
    at cachedError (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/query.js:170:23)
    at new Query (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/query.js:36:24)
    at sql (file:///node_modules/.pnpm/[email protected]/node_modules/postgres/src/index.js:112:11)

If I remove either one of the CTEs, it works fine. And with both CTEs it works fine when run in a Postgres client.
My scenario resembles that of #913, except I don't have any INSERT in my CTEs.

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