drizzle-team / drizzle-team/drizzle-orm
[BUG]: drizzle-orm/sqlite-proxy creates conflicts with aliased columns
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] I have verified that the bug I'm about to report hasn't been filed before.
### What version of `drizzle-orm` are you using?
"drizzle-orm": "^0.41.0",
### What version of `drizzle-kit` are you using?
"drizzle-kit": "^0.30.6"
### Other packages
_No response_
### Describe the Bug
With `drizzle-orm/sqlite-proxy` if I do:
```js
return await db.select({
id: schema.user.id,
name: schema.user.name,
team_id: schema.teams.id
})
.from(schema.users)
.innerJoin(schema.teams, eq(schema.teams.id, schema.users.team_id))
```
My result get all mixed up (e.g. the team_id is assigned to user_id etc).
I use the proxy for D1 while local as:
```js
const d1LiveProxy = () => {
console.warn('Using proxy production database')
if (!import.meta.dev){
throw createError({statusCode:400, statusMessage:"Can't use proxy in production"})
}
return drizzleProxy(async (sql, params, method) => {
const url = `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/d1/database/${process.env.CLOUDFLARE_D1_ID}/query`
const resp = await fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.CLOUDFLARE_D1_TOKEN}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ sql, params, method })
}).catch(e => {
console.log("Proxy error")
})
const data = await resp.json()
if (data?.errors?.length > 0 || !data?.success) {
throw new Error(`Error from sqlite proxy server: \n${JSON.stringify(data)}}`)
}
const qResult = data.result[0]
if (!qResult?.success) {
throw new Error(`Error from sqlite proxy server: \n${JSON.stringify(data)}`)
}
return { rows: qResult.results.map(r => Object.values(r)) }
})
}
```
This is a serious bug which could lead to a ton of issues e.g. saving a user with a wrong ID etc
Contributor guide
Research direction
Start at the drizzle-orm/sqlite-proxy entry point and reproduce the aliased-column join using the supplied select, innerJoin, and Object.values(r) proxy response. Trace how returned column order is mapped to selected aliases, then add a regression test for the shown query; done means id, name, and team_id retain the correct values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sqlite, typescript
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100