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
24.55k stars 644 forks source link

[BUG]: undefined field occurs when using full join without specify select field #1835

Open miello opened 9 months ago

miello commented 9 months ago

What version of drizzle-orm are you using?

0.29.3

What version of drizzle-kit are you using?

0.20.13

Describe the Bug

It looks like that when using full join without specify select field, the result which return from query does not have value from the last column in joined table. This behavior happens in drizzle-orm/bun-sqlite

const queryData = async () => {
  const projectEntry = await db
    .select()
    .from(schema.projects)
    .fullJoin(schema.users, eq(schema.users.userId, schema.projects.userId))
    .where(eq(schema.users.userId, "1234"));

  console.log(projectEntry); // <- This one returns missing users.testing

  const projectEntry2 = await db
    .select({
      userId: schema.users.userId,
      projectId: schema.projects.projectId,
      data: schema.users.data,
      testing: schema.users.testing
    })
    .from(schema.projects)
    .fullJoin(schema.users, eq(schema.users.userId, schema.projects.userId))
    .where(eq(schema.users.userId, "1234"));

  console.log(projectEntry2); // <- users.testing is found
};

if we clone the repository below and run it, it will give these results

[
  {
    projects: {
      userId: "1234",
      projectId: "Hello World",
    },
    users: {
      userId: "1234",
      data: "Hello world",
      testing: undefined,
    },
  }
]
[
  {
    userId: "1234",
    projectId: "Hello World",
    data: "Hello world",
    testing: "Hello 1234",
  }
]

Expected behavior

users.testing value should not be undefined

[
  {
    projects: {
      userId: "1234",
      projectId: "Hello World",
    },
    users: {
      userId: "1234",
      data: "Hello world",
      testing: "Hello 1234",
    },
  }
]

Environment & setup

varugasu commented 8 months ago

Same problem. Created a repository for it. See README for more details

What version of drizzle-orm are you using? 0.30.1

What version of drizzle-kit are you using? 0.20.14

Environment & setup

varugasu commented 8 months ago

Debugging a little more, I can see the problem is the return of mapResultRow. We are executing the right query, the query is returning the correct values, but the return of mapResultRow is not correctly mapping the values (table_a.column_b and table_a.created_at in my example)

image

I will investigate https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/utils.ts#L14-L58 more later