ORM relation `where` does not exist in type

Can someone help me understand if I'm doing something wrong? The following code (adapted from Query/Select filters gives a 'does not exist' error on where:
export async function getItem(id: string, userId: string) {
const result = await db.query.item.findFirst({
where: eq(t.item.id, id),
with: {
collection: {
where: eq(t.collection.userId, userId)
}
}
});
return result;
}
export async function getItem(id: string, userId: string) {
const result = await db.query.item.findFirst({
where: eq(t.item.id, id),
with: {
collection: {
where: eq(t.collection.userId, userId)
}
}
});
return result;
}
Object literal may only specify known properties, and 'where' does not exist in type..
If schema matters, I have a user table, a collection table with fk (userId) and a item table with fk (collectionId). I should note that the query works, so maybe it's lacking proper types?
9 Replies
TOSL
TOSL2mo ago
try this
import {item, collection} from './db/schema'

export async function getItem(id: string, userId: string) {
const result = await db.query.item.findFirst({
where: eq(item.id, id),
with: {
collection: {
where: eq(collection.userId, userId)
}
}
});
return result;
}
import {item, collection} from './db/schema'

export async function getItem(id: string, userId: string) {
const result = await db.query.item.findFirst({
where: eq(item.id, id),
with: {
collection: {
where: eq(collection.userId, userId)
}
}
});
return result;
}
basically you need to import the tables from your schema where ever it may be
.frag
.fragOP2mo ago
I already had them imported with import * as t from.., I tried your method of importing (destructuring?) but no luck
scape
scape2mo ago
as far as I know, nested where currently is not supported with queries on the other hand, you can make it using select, fe
db.select()
.from(item)
.where(and(eq(item.id, id), eq(collection.userId, userId))
.innerJoin(collection, eq(collection.itemId, item.Id))
db.select()
.from(item)
.where(and(eq(item.id, id), eq(collection.userId, userId))
.innerJoin(collection, eq(collection.itemId, item.Id))
.frag
.fragOP2mo ago
hi @scape , thanks for the response, that's what I thought as well and used inner joins.. however I find this strange since this is clearly documented? https://orm.drizzle.team/docs/rqb#select-filters
TOSL
TOSL2mo ago
Try the callback syntax
const users = await db.query.users.findMany({
where: eq(users.id, 1)
})

//callback
const users = await db.query.users.findMany({
where: (users, { eq }) => eq(users.id, 1),
})
const users = await db.query.users.findMany({
where: eq(users.id, 1)
})

//callback
const users = await db.query.users.findMany({
where: (users, { eq }) => eq(users.id, 1),
})
Also can you share your schema? It could be that you're trying to filter on one relation. Drizzle doesn't support filtering on a one relation, it would basically return null.
TOSL
TOSL2mo ago
GitHub
[BUG]: Filtering results based on relation content using RQB · Issu...
What version of drizzle-orm are you using? 0.29.1 What version of drizzle-kit are you using? 0.20.4 Describe the Bug There's been a few threads on Discord about it but no solid answer other tha...
.frag
.fragOP2mo ago
Okay I see, I'll watch the issue on github, thanks.
TOSL
TOSL2mo ago
It's not an error. It's the expected result. I just wanted you to see a source.
.frag
.fragOP2mo ago
Yes I got that, sorry if my response was misleading. I meant that I'll be tracking the progress as I've noticed other people requesting improvements to the relational query API.

Did you find this page helpful?