drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.57k stars 490 forks source link

[BUG]: wrong typeHint when using relations (one and automatic limit: 1) #1368

Open benjamine opened 8 months ago

benjamine commented 8 months ago

What version of drizzle-orm are you using?

0.28.6

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

when using a relation one-to-one, and a filter by id over the main entity,

eg. querying posts with their author.

it seems like drizzle-orm automatically adds a limit: 1 to the join (which makes sense). but apparently this adds a new param to the query (for that 1) which seems to offset the typeHint of params later.

the query:

export const getArticlesByWorkspace = async (workspaceId: string) =>
  db.query.articleTable.findMany({
    with: {
      updatedByUser: {
        columns: {
          name: true,
          email: true,
        },
      },
    },
    columns: {
      id: true,
      name: true,
      description: true,
      createdAt: true,
      createdBy: true,
      updatedAt: true,
      updatedBy: true,
    },
    where: and(eq(articleTable.workspaceId, workspaceId)),
    orderBy: [articleTable.name],
    limit: 1000,
  });

(where articleTable has a one-to-one relationship with userTable, based on the updatedBy field in articleTable)

here's the failing query:

Query: select "articleTable"."id", "articleTable"."name", "articleTable"."description", "articleTable"."createdAt", "articleTable"."createdBy", "articleTable"."updatedAt", "articleTable"."updatedBy", "articleTable_updatedByUser"."data" as "updatedByUser" from "article" "articleTable" left join lateral (select json_build_array("articleTable_updatedByUser"."name", "articleTable_updatedByUser"."email") as "data" from (select * from "user" "articleTable_updatedByUser" where "articleTable_updatedByUser"."id" = "articleTable"."updatedBy" limit :1) "articleTable_updatedByUser") "articleTable_updatedByUser" on true where "articleTable"."workspace_id" = :2 order by "articleTable"."name" limit :3 -- params: [{"name":"1","value":{"longValue":1},"typeHint":"UUID"}, {"name":"2","value":{"stringValue":"56cc697c-ab59-4ab1-bc34-4bc4013f9dcd"}}, {"name":"3","value":{"longValue":1000}}]

Expected behavior

"typeHint":"UUID" should be on param 2 (not in 1)

Environment & setup

I'm using AWS Data API, aurora postgres

pantoninho commented 4 months ago

I'm experiencing the same issue.

cmanou commented 3 months ago

I also noticed this in an insert where I use a sql value for one of the values the params after all have offset type hints