Skip to content

Invalid Raw() query in postgresql in jsonb fields #11611

@warang580

Description

@warang580

Issue description

Raw() query on a jsonb field in Postgresql doesn't escape correctly columnAlias with quotes

Expected Behavior

I'm trying to query entities based on jsonb properties.

@Entity()
export class PostAuthor {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column('jsonb')
  meta: any;
}

In database I have something like

author1.meta.traits = ['cool',  'fun']
author2.meta.traits = ['smart', 'nice']

And I'm trying to get only authors with a specific trait.
(This is a simple example that could be done differently but I need to make it work in jsonb because I need more complex needs later on with jsonb)

When I try to query using Raw(), jsonb field meta has incorrect generated SQL.

const authors = await this.postAuthorsRepository.find({
      where: {
        // It correctly escapes columnAlias with quotes on a non-jsonb field
        name: Raw((columnAlias) => `${columnAlias} LIKE :authorName`, {
          authorName: 'Jane%',
        }),

        // It doesn't espace columnAlias correctly on a jsonb field
        meta: Raw((columnAlias) => {
            return `${columnAlias}->'traits' ? :trait`;
            // gives
            return `PostAuthor.meta->'traits' ? :trait`;
            // instead of
            return `"PostAuthor"."meta"->'traits' ? :trait`;
            // or at least
            return `"PostAuthor".meta->'traits' ? :trait`;
          },
          { trait: 'fun' },
        ),
      },
    });

Actual Behavior

This is the generated query :

query: `SELECT "PostAuthor"."id" AS "PostAuthor_id", "PostAuthor"."name" AS "PostAuthor_name", "PostAuthor"."meta" AS "PostAuthor_meta", "PostAuthor"."created_at" AS "PostAuthor_created_at", "PostAuthor"."updated_at" AS "PostAuthor_updated_at" FROM "s01_post_author" "PostAuthor" WHERE (("PostAuthor"."name" LIKE $1) AND (PostAuthor.meta->'traits' ? $2))`,
  parameters: [
    'Jane%',
    'fun'
  ],

and the associated error :

driverError: error: missing FROM-clause entry for table "postauthor"

because it has generated (PostAuthor.meta->'traits' ? $2) instead of ("PostAuthor"."meta"->'traits' ? $2) like it did for name field in ("PostAuthor"."name" LIKE $1)

Steps to reproduce

@Entity()
export class SomeEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('jsonb')
  meta: any;
}

and

entityRepository.find({
  where: {
    meta: Raw((columnAlias) => {
      return `${columnAlias}->'traits' ? :trait`;
    }, { trait: 'fun' }),
  },
})

My Environment

Dependency Version
Operating System Ubuntu 24.04.2 LTS
Node.js version 20.17.0
Typescript version 5.7.3
TypeORM version 0.3.25
NestJS version 11.0.1
NestJS TypeORM version 11.0.0

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions