-
Notifications
You must be signed in to change notification settings - Fork 772
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: when trying to left join 2 tables that have same name columns - second column is not returned #3160
Comments
👋🏻 Greetings, Feel free to check out the Cloudflare Discord where there is a channel for D1 as well: https://discord.com/invite/cloudflaredev Cc: @rozenmd |
@AndriiSherman could I bother you for a schema/db sql file to reproduce this? |
@rozenmd Although I am not Andrii, I think I have the same problem and here's a minimal example schema that has the issue (pretty much any schema that you can join where both have one shared field name (here it's "id"): import type { InferModel } from "drizzle-orm";
import {
integer, primaryKey,
sqliteTable,
text
} from "drizzle-orm/sqlite-core";
export const users = sqliteTable(
"users",
{
id: integer("id").primaryKey({
autoIncrement: true
}),
email: text("email").notNull(),
}
);
export const subscriptions = sqliteTable(
"subscriptions",
{
id: integer("id").primaryKey({ autoIncrement: true }),
user: integer("user").notNull().references(() => users.id, {
onDelete: "cascade"
})
}
); |
Agreed. Any SELECT query involving joins and having the same fields in the response will not work as expected. In our case, we are using the raw mode for querying, but it appears that the raw mode in the "d1" driver differs from the raw mode in other drivers Link to a code: https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/templates/d1-beta-facade.js#L177 After receiving the response in JSON format, it is subsequently mapped to an array of arrays at the code level. Consequently, fields with the same name will be overridden. |
We've traced this back to workerd, and will be discussing possible solutions here: cloudflare/workerd#696 Keep in mind @AndriiSherman that a temporary fix on Drizzle's side is still possible by using |
I’m pretty sure it behaves wrongly even with this option. I’m gonna need to check, but how could it return multiple identical keys in an array of objects? Normally SQL databases return the table name in the joined table’s column names. Or even in both. Do you want me to check, even if it’s impossible to return multiple keys of the same name, @rozenmd? |
We have the same problem, does anything block fix for this bug? |
We are working on this. Current priority is larger databases, which is the biggest ask from most users. |
Thanks for a prompt response @elithrar! |
Is there any update on resolving this issue? I have tables with the same names for common fields like created_at, updated_at, id etc and joins don't work because all the response data is shifting columns in the results. |
FYI, this will be fixed D1 once cloudflare/workerd#1586 is merged and rolls out It will only fix the results of |
This is now released. |
Changelog: https://developers.cloudflare.com/d1/platform/changelog/#2024-02-13 cc @AndriiSherman on the Drizzle team. |
Has it been released already? I would like to run some tests, can't find this version for types on npm |
@AndriiSherman sorry for the delay there - the types are released in |
@rozenmd I've tested this with latest types version ( Here's a reproduction repo: https://github.com/dankochetov/drizzle-d1-bug |
Hi @dankochetov, that looks like a bug in local mode (probably related to The I'll take a look at the local behaviour this week, apologies for the oversight. |
@geelen I'm using the latest wrangler Is there anything special I need to do to get the new version? |
Running my query from the D1 Dashboard console UI or wrangler D1 CLI get me the same, buggy result. It collapses columns with the same name from joined tables into a single one, the last joined table seemingly taking precedence. SELECT id, A.description, B.description FROM table_a A LEFT JOIN table_b B ON B.id = A.id WHERE id = 'abc' |
Are there any updates on this? |
Miniflare squashes columns with the same name, which is inconsistent with expected behavior and production where the problem seemingly was fixed.
I added a test case for the problem: #5917 I went down a rabbit hole chasing this bug through Hopefully someone from the maintainers will notice it and help me fix the problem. Any tips will be appriciated. |
The issue: cloudflare/workers-sdk#3160 The test is passing and it is proof of the expected behavior that Workers SDK doesn't match.
The issue: cloudflare/workers-sdk#3160 The test is passing and it is proof of the expected behavior that Workers SDK doesn't match.
This PR fixes cloudflare#3160 caused by ignoring the `resultsFormat` query that `workerd` sends. The commits add proper handling of the query. It makes `raw` preserve columns with the same name by handling `resultsFormat=ROWS_AND_COLUMNS` and removes `results` from `run` as it sends `resultsFormat=NONE` (see https://github.com/cloudflare/workerd/blob/1d89f3b8e9cdcd898ea486656d72d9551e79f4a3/src/cloudflare/internal/d1-api.ts#L295)
I found the root cause of the problem and fixed it: #5917 Hopefully, the PR will be merged and shipped soon. |
This PR fixes cloudflare#3160 caused by ignoring the `resultsFormat` query that `workerd` sends. The commits add proper handling of the query. It makes `raw` preserve columns with the same name by handling `resultsFormat=ROWS_AND_COLUMNS` and removes `results` from `run` as it sends `resultsFormat=NONE` (see https://github.com/cloudflare/workerd/blob/1d89f3b8e9cdcd898ea486656d72d9551e79f4a3/src/cloudflare/internal/d1-api.ts#L295)
@kossnocorp - that PR specifically fixes Workers when run locally, not the execute command. I'll have a follow-up PR for the execute command shortly. |
* Handle resultsFormat in D1 worker This PR fixes #3160 caused by ignoring the `resultsFormat` query that `workerd` sends. The commits add proper handling of the query. It makes `raw` preserve columns with the same name by handling `resultsFormat=ROWS_AND_COLUMNS` and removes `results` from `run` as it sends `resultsFormat=NONE` (see https://github.com/cloudflare/workerd/blob/1d89f3b8e9cdcd898ea486656d72d9551e79f4a3/src/cloudflare/internal/d1-api.ts#L295) * Handle resultsFormat in D1 worker This PR fixes #3160 caused by ignoring the `resultsFormat` query that `workerd` sends. The commits add proper handling of the query. It makes `raw` preserve columns with the same name by handling `resultsFormat=ROWS_AND_COLUMNS` and removes `results` from `run` as it sends `resultsFormat=NONE` (see https://github.com/cloudflare/workerd/blob/1d89f3b8e9cdcd898ea486656d72d9551e79f4a3/src/cloudflare/internal/d1-api.ts#L295) * Update packages/miniflare/test/plugins/d1/test.ts * Ignore NONE format behaviour in D1 worker See comment: #5917 (comment) * Add passing test for new bindings * Update .changeset/little-jars-train.md * Update .changeset/little-jars-train.md * Update .changeset/little-jars-train.md * Update .changeset/little-jars-train.md * Update little-jars-train.md --------- Co-authored-by: Max Rozen <3822106+rozenmd@users.noreply.github.com>
Ugh GitHub - nope not fixed yet |
Which Cloudflare product(s) does this pertain to?
D1
What version of
Wrangler
are you using?2.19.0
What operating system are you using?
macOS
Describe the Bug
As per issue #555 we can see, that d1 stopped returning duplicate names from different tables. It was working before and it's how any other driver is doing for
raw
modeBut should return
id
for user andid
for posts as wellExpected
┌────┬────┬─────────┬───────┬───────────┐ │ id │ id │ ownerId │ title │ content │ ├────┼────┼─────────┼───────┼───────────┤ │ 2 │ 1 │ 1 │ test │ 111111111 │ ├────┼────┼─────────┼───────┼───────────┤ │ 3 │ 2 │ 1 │ ueoau │ ueueu │ ├────┼────┼─────────┼───────┼───────────┤ │ 1 │ 3 │ 1 │ xxx │ yyy │ └────┴────┴─────────┴───────┴───────────┘
The text was updated successfully, but these errors were encountered: