Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: relation query API default alias is different than regular alias #2431

Open
foxfriends opened this issue Jun 3, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@foxfriends
Copy link

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.4

Describe the Bug

In a query such as the one below the table is named agent_activity so when I reference it as schema.agentActivity in the subquery that gets generated as agent_activity, but the drizzle.query.agentActivity includes an alias for agentActivity, not the same as the name used in the subquery, so I have to put in a manual alias so that the names line up

// schema.ts (relevant excerpt)
export const agentActivity = pgTable(
  "agent_activity",
  {
    id: uuid("id")
      .default(sql`uuid_generate_v4()`)
      .primaryKey()
      .notNull(),
    agentId: uuid("agent_id")
      .notNull()
      .references(() => agents.id, { onDelete: "cascade", onUpdate: "cascade" }),
  }
);
export const agentActivityResult = pgTable(
  "agent_activity_result",
  {
    id: uuid("id")
      .default(sql`uuid_generate_v4()`)
      .primaryKey()
      .notNull(),
    activityId: uuid("activity_id")
      .notNull()
      .references(() => agentActivity.id, { onDelete: "cascade", onUpdate: "cascade" }),
  }
);


// My query:
const activities = await drizzle.query.agentActivity.findMany({
  where: and(
    eq(schema.agentActivity.agentId, "some-uuid"),
    notExists(
      drizzle
        .select()
        .from(schema.agentActivityResult)
        .where(
          // HACK: drizzle.query.agentActivity aliases the table as "agentActivity", but this subquery by default calls it "agent_activity" so we hack around...
          eq(schema.agentActivityResult.activityId, alias(schema.agentActivity, "agentActivity").id),
        ),
    ),
  ),
});

This example was copied out of my code (with some omissions), but an equivalent will be similar I'm sure. I can provide a minimal reproduction if necessary

Expected behavior

The default names of aliases should be the same in the drizzle.query API as in the regular query builder so that this query works by default without needing alias

Environment & setup

This issue is in the query builder level, I am querying on a PostgreSQL database.

@foxfriends foxfriends added the bug Something isn't working label Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant