drizzle-team / drizzle-team/drizzle-orm
[BUG]: shared column definition cause wrong column name generated in relation query v2
- 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?
1.0.0-beta.20
### What version of `drizzle-kit` are you using?
N/A
### Other packages
better-sqlite3@12.8.0
### Describe the Bug
## the documents introduced shared column definition
https://orm.drizzle.team/docs/sql-schema-declaration#advanced
but when shared column definition used in relation query v2, wrong column name generated, see below
## sqlite DDL
```sql
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
old_names TEXT
);
CREATE TABLE posts (
id INTEGER PRIMARY KEY,
uid INTEGER,
title TEXT,
keywords TEXT,
FOREIGN KEY (uid) REFERENCES users(id)
);
INSERT INTO users (id, name, old_names) VALUES
(1, 'Alice', '["A","Al"]'),
(2, 'Bob', '["Bobby"]'),
(3, 'Charlie', '[]');
INSERT INTO posts (id, uid, title, keywords) VALUES
(1, 1, 'Hello World', '["intro","welcome"]'),
(2, 1, 'Advanced Topics', '["drizzle","orm"]'),
(3, 2, 'Bob Post', '["random"]');
```
## schema & relations
```ts
#!/usr/bin/env tsx
import { defineRelations } from 'drizzle-orm'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
const stringArrayDefinition = text({ mode: 'json' }).$type()
const stringArrayDefinitionFn = () => text({ mode: 'json' }).$type()
const users = sqliteTable('users', {
id: integer().primaryKey(),
name: text(),
oldNames: stringArrayDefinitionFn(),
})
const posts = sqliteTable('posts', {
id: integer().primaryKey(),
uid: integer().references(() => users.id),
title: text(),
keywords: stringArrayDefinitionFn(),
})
const relations = defineRelations({ users, posts }, (r) => {
return {
users: {
posts: r.many.posts({
from: r.users.id,
to: r.posts.uid,
}),
},
}
})
const db = drizzle({ connection: __dirname + '/test.db', casing: 'snake_case', relations })
void (async () => {
console.log(
await db.query.users.findFirst({
with: { posts: true },
}),
)
})()
```
### when using `stringArrayDefinitionFn()`
outputs
```txt
[prepare] sql: select "d0"."id" as "id", "d0"."name" as "name", "d0"."old_names" as "oldNames", coalesce((select json_group_array(json_object('id', "id", 'uid', "uid", 'title', "title", 'keywords', "keywords")) as "r" from (select "d1"."id" as "id", "d1"."uid" as "uid", "d1"."title" as "title", "d1"."keywords" as "keywords" from "posts" as "d1" where "d0"."id" = "d1"."uid") as "t"), jsonb_array()) as "posts" from "users" as "d0" limit ?
{
id: 1,
name: 'Alice',
oldNames: [ 'A', 'Al' ],
posts: [
{ id: 1, uid: 1, title: 'Hello World', keywords: [Array] },
{ id: 2, uid: 1, title: 'Advanced Topics', keywords: [Array] }
]
}
```
### when using shared `stringArrayDefinition`
```txt
[prepare] sql: select "d0"."id" as "id", "d0"."name" as "name", "d0"."old_names" as "oldNames", coalesce((select json_group_array(json_object('id', "id", 'uid', "uid", 'title', "title", 'keywords', "keywords")) as "r" from (select "d1"."id" as "id", "d1"."uid" as "uid", "d1"."title" as "title", "d1"."old_names" as "keywords" from "posts" as "d1" where "d0"."id" = "d1"."uid") as "t"), jsonb_array()) as "posts" from "users" as "d0" limit ?
/root/node_modules/.pnpm/better-sqlite3@12.8.0/node_modules/better-sqlite3/lib/methods/wrappers.js:6
return this[cppdb].prepare(sql, this, false);
^
SqliteError: no such column: d1.old_names
```
## env
- node.js
- better-sqlite3
- relations query v2
I don't know which are relevant
Contributor guide
Assessment
This issue has not been assessed yet.