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]: Where clauses don't take into account the table name #3153

Open
ck-euan opened this issue Oct 18, 2024 · 0 comments
Open

[BUG]: Where clauses don't take into account the table name #3153

ck-euan opened this issue Oct 18, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ck-euan
Copy link

ck-euan commented Oct 18, 2024

What version of drizzle-orm are you using?

0.35.1

What version of drizzle-kit are you using?

0.26.2

Describe the Bug

I was trying to implement the new count API following the example from the release notes:

 await ctx.db.query.customers.findMany({
      extras: {
	      tasksCount: ctx.db
		      .$count(
			      tasks,
			      eq(tasks.customerId, customers.id),
		      )
		      .as("tasksCount"),
      },
});

However, this resulted in the following subquery:
(select count(*) from "tasks" where "customers"."customer_id" = "customers"."id")

and inevitably gave this error:

"Unknown column 'customers.customer_id' in 'where clause'"

This is quite confusing, and perhaps a bit misleading because you'd assume that tasks.customerId would mean the customer id from the tasks table.

To get it to work I had to change it to this:

tasksCount: ctx.db
		.$count(
			tasks,
			sql`\`tasks\`.\`customer_id\` = \`customers\`.\`id\``,
		)
		.as("tasksCount"),

Expected behavior

When you import a table and use it in a where clause it should take into account the table name, not just the column name.

For example:

eq(tasks.customerId, customers.id)

should be equivalent to

sql`\`tasks\`.\`customer_id\` = \`customers\`.\`id\`

Environment & setup

No response

@ck-euan ck-euan added the bug Something isn't working label Oct 18, 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