-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
When using findManyAndCount on a view (SQLite dialect) the count() incorrectly refers to a source table column causing SQLiteError: no such column
Here, kv_view is a view and kv is a source table referred to by the view.
{
sql: "select count(*) AS \"count\" from \"kv_view\" where \"kv\".\"key\" = ?",
params: [ "foo" ],
typings: [ "none" ],
}Using drizzle-orm 1.0.0-beta.8 and bun 1.3.5.
See below for full repro.
import { Database } from 'bun:sqlite'
import { describe, expect, it } from 'bun:test'
import { defineRelations } from 'drizzle-orm'
import { drizzle } from 'drizzle-orm/bun-sqlite'
import { integer, real, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core'
import 'drizzle-plus/sqlite/findManyAndCount'
import 'drizzle-plus/sqlite/count'
describe('Drizzle', async () => {
const kv = sqliteTable('kv', {
id: integer().primaryKey({ autoIncrement: true }),
key: text().notNull(),
value: real().notNull(),
})
const kvView = sqliteView('kv_view').as(qb => qb.select().from(kv))
const schema = { kv, kvView }
const relations = defineRelations(schema)
const client = new Database(':memory:', { create: true })
const db = drizzle({ client, relations })
db.run(`CREATE TABLE IF NOT EXISTS kv (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT NOT NULL,
value REAL NOT NULL
)`)
db.run(`CREATE VIEW IF NOT EXISTS kv_view AS SELECT * FROM kv`)
await db.insert(kv).values([
{ key: 'foo', value: 1 },
{ key: 'bar', value: 2 },
])
describe('drizzle-plus findManyAndCount bug', async () => {
it('findManyAndCount works on table', async () => {
const q = db.query.kv.findManyAndCount({ where: { key: 'foo' } })
console.log(q.toSQL().findMany)
console.log(q.toSQL().count)
const res = await q
expect(res.count).toBe(1)
expect(res.data).toHaveLength(1)
})
it('vanilla count works on table', async () => {
const q = db.query.kv.count({ key: 'foo' })
console.log(q.toSQL())
const res = await q
expect(res).toBe(1)
})
it('findManyAndCount works on view', async () => {
const q = db.query.kvView.findManyAndCount({ where: { key: 'foo' } })
console.log(q.toSQL().findMany)
console.log(q.toSQL().count)
const res = await q // fails here: SQLiteError: no such column: kv.key
expect(res.count).toBe(1)
expect(res.data).toHaveLength(1)
})
it('vanilla count works on view', async () => {
const q = db.query.kvView.count({ key: 'foo' })
console.log(q.toSQL())
const res = await q // fails here: SQLiteError: no such column: kv.key
expect(res).toBe(1)
})
})
})Metadata
Metadata
Assignees
Labels
No labels