Nested where clauses in relational queries
I have this query:
const asset = await db.query.assets.findFirst({
with: {
assetImages : true,
assetModeration: {
where: eq(assetsModerations.state, "ACCEPTED"), //this line gives error
columns: {
state: true,
},
}
},
where: (eq(assets.id, id))
})
that gives this error: Object literal may only specify known properties, and 'where' does not exist in type ...
the assetsModerations table looks like this:
export const assetsModerations = pgTable(
"assets_moderations",
{
id: serial('id').primaryKey(),
created_at: timestamp('created_at').notNull().defaultNow(),
description: text('description'),
moderatorId: text("moderator_id").references(() => users.id),
updatedAt: timestamp('updated_at'),
state: moderationState('moderation_state').notNull().default('PENDING')
}
)
this error is not given if i try to do the same with assetImages, the other table, what is the problem here?
Thank you in advance.
27 Replies
Relational queries – DrizzleORM
Drizzle ORM | %s
unfortunately it does not work, it just says 'where' property does not exist on assetsModerations
What do your relations look like? (to wrap code blocks you can use ``` on both sides of it!)
thank you! i'm sending them now
thats it, its a one on one relationship
one asset -> one assetModeration
What version of
drizzle-orm
do you have?[email protected]
should i npm update? i saw a new version got released today
if you're able to update that'd be great, right now the latest is
0.28.5
ok I'm updating, trying again in a minute! thank you for the help btw <3
still no 'where' unfortunately :(
here after inserting the expression
the only thing that comes to my mind that could be causing this is that every column of this table is optional, when inserting i leave the values blank, most of them have defaults
What if you try what Andrew suggested (replace
eq(assets.assetCategoryId, category.id)
with (table) => eq(assets.assetCategoryId, category.id)
)
if that doesn't work, can you send your assets schema?you mean like this?
sorry, misread the line. replace the line with the error with that
yes sure, here it is:
so it'd be
(table) => eq(table.state, "ACCEPTED")
still getting the error :(
@Andrew Sherman I'm able to reproduce this in the drizzle-playground repo, with and without the callback syntax. Not sure where to go from there. Can send a sample repo in a bit.
@Dan Kochetov just in case you can spot an issue quickly
every time
you cannot filter a
one
relation🫡
🤦♂️makes sense
i did not mean to filter the relationship, i did not understand well how to use relational queries, what i want to do is to filter every asset for which its moderation is not accepted, i want to filter the top layer table
every asset has one assetModeration, i want to filter out assets that do not have assetModeration.state === "accepted"
Unfortunately filtering by nested relations is not currently possible. You'll have to either run the query and filter by the nested relations on the code side, or you'll have to use the Core API and joins/filter "manually"
ahhh thank you. Do you know if this is a feature that is planned for the future or not?
we're discussing it internally for now, it's possible it'll be implemented in some form in the future
ok thank you!