Skip to content

[BUG]: Incorrect aliasing in queries with raw where clause #5186

@hlysine

Description

@hlysine

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

1.0.0-beta.4-4605abe

What version of drizzle-kit are you using?

1.0.0-beta.6-4414a19

Other packages

No response

Describe the Bug

I get an error from PGlite when I run the following query:

const user = await db.query.users.findFirst({
  columns: {
    id: true,
  },
  with: {
    createdPuzzles: {
      columns: {
        id: true,
        createdAt: true,
        updatedAt: true,
        title: true,
        description: true,
        puzzleCount: true,
        followCount: true,
        status: true,
        autoPopulate: true,
        modifiedAt: true,
        isSeries: true,
      },
    },
  },
  where: {
    RAW: isNotNull(users.createdPuzzlesId),
  },
});
This is the error returned by PGlite
error: invalid reference to FROM-clause entry for table "users"
     length: 185,
   severity: "ERROR",
     detail: undefined,
       hint: "Perhaps you meant to reference the table alias \"d0\".",
   position: "1041",
 internalPosition: undefined,
 internalQuery: undefined,
      where: undefined,
     schema: undefined,
      table: undefined,
   dataType: undefined,
 constraint: undefined,
       file: "parse_relation.c",
    routine: "errorMissingRTE",
      query: "\n      select\n        \"d0\".\"id\" as \"id\",\n        \"createdPuzzles\".\"r\" as \"createdPuzzles\"\n      from\n        \"users\" as \"d0\"\n        left join lateral (\n          select\n            row_to_json (\"t\".*) \"r\"\n          from\n            (\n              select\n                \"d1\".\"id\" as \"id\",\n                \"d1\".\"created_at\" as \"createdAt\",\n                \"d1\".\"updated_at\" as \"updatedAt\",\n                \"d1\".\"title\" as \"title\",\n                \"d1\".\"description\" as \"description\",\n                \"d1\".\"puzzle_count\" as \"puzzleCount\",\n                \"d1\".\"follow_count\" as \"followCount\",\n                \"d1\".\"status\" as \"status\",\n                \"d1\".\"auto_populate\" as \"autoPopulate\",\n                \"d1\".\"modified_at\" as \"modifiedAt\",\n                \"d1\".\"is_series\" as \"isSeries\"\n              from\n                \"collections\" as \"d1\"\n              where\n                \"d0\".\"created_puzzles_id\" = \"d1\".\"id\"\n              limit\n                1\n            ) as \"t\"\n        ) as \"createdPuzzles\" on true\n      where\n        (\"users\".\"created_puzzles_id\" is not null)\n      limit\n        1\n    ",
     params: [],
 queryOptions: {
  rowMode: "object",
  parsers: [Object ...],
}
This is the SQL generated by Drizzle
select
  "d0"."id" as "id",
  "createdPuzzles"."r" as "createdPuzzles"
from
  "users" as "d0"
  left join lateral (
    select
      row_to_json ("t".*) "r"
    from
      (
        select
          "d1"."id" as "id",
          "d1"."created_at" as "createdAt",
          "d1"."updated_at" as "updatedAt",
          "d1"."title" as "title",
          "d1"."description" as "description",
          "d1"."puzzle_count" as "puzzleCount",
          "d1"."follow_count" as "followCount",
          "d1"."status" as "status",
          "d1"."auto_populate" as "autoPopulate",
          "d1"."modified_at" as "modifiedAt",
          "d1"."is_series" as "isSeries"
        from
          "collections" as "d1"
        where
          "d0"."created_puzzles_id" = "d1"."id"
        limit
          1
      ) as "t"
  ) as "createdPuzzles" on true
where
  ("users"."created_puzzles_id" is not null)
limit
  1
This is what Drizzle should have generated
  select
    "d0"."id" as "id",
    "createdPuzzles"."r" as "createdPuzzles"
  from
    "users" as "d0"
    left join lateral (
      select
        row_to_json ("t".*) "r"
      from
        (
          select
            "d1"."id" as "id",
            "d1"."created_at" as "createdAt",
            "d1"."updated_at" as "updatedAt",
            "d1"."title" as "title",
            "d1"."description" as "description",
            "d1"."puzzle_count" as "puzzleCount",
            "d1"."follow_count" as "followCount",
            "d1"."status" as "status",
            "d1"."auto_populate" as "autoPopulate",
            "d1"."modified_at" as "modifiedAt",
            "d1"."is_series" as "isSeries"
          from
            "collections" as "d1"
          where
            "d0"."created_puzzles_id" = "d1"."id"
          limit
            1
        ) as "t"
    ) as "createdPuzzles" on true
  where
-   ("users"."created_puzzles_id" is not null)
+   ("d0"."created_puzzles_id" is not null)
  limit
    1

I have a setup that relies extensively on raw where clauses, which is completely broken by this bug. Hopefully this can be fixed asap, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions