Object literal may only specify known properties, and photo does not exist in type

My query is
const res = await drizzleClient
.select({
...getTableColumns(users),
userAbout: {
...getTableColumns(userAbout),
avatar: {
...getTableColumns(avatars),
photo: {
...getTableColumns(photos),
}
}
}
})
.from(users)
.leftJoin(userAbout, eq(userAbout.uid, users.uid))
.leftJoin(avatars, eq(avatars.phid, userAbout.avatarPhid))
.leftJoin(photos, eq(photos.phid, avatars.phid))
.where(eq(users.uid, 2))
const res = await drizzleClient
.select({
...getTableColumns(users),
userAbout: {
...getTableColumns(userAbout),
avatar: {
...getTableColumns(avatars),
photo: {
...getTableColumns(photos),
}
}
}
})
.from(users)
.leftJoin(userAbout, eq(userAbout.uid, users.uid))
.leftJoin(avatars, eq(avatars.phid, userAbout.avatarPhid))
.leftJoin(photos, eq(photos.phid, avatars.phid))
.where(eq(users.uid, 2))
Near photo in select I get the type error
Object literal may only specify known properties, and photo does not exist in type
Object literal may only specify known properties, and photo does not exist in type
My IDE is webstorm 2023.3.4 The result of this query is successful (screenshot in attach) How to deal with this type error?
No description
11 Replies
Aaroned
Aaroned8mo ago
@andreas_444 have you included photo in the relations for the avatar table?
andreas_444
andreas_444OP8mo ago
@Aaroned phid in avatar isnt fk. And I prefer not to use fk in this case
Huge Letters
Huge Letters8mo ago
can't you just do photo: getTableColumns(photos)? should be easier on the type-checker also is this reproducible in typescript playground? you dont have to post it here - just check yourself if the error is still there in other environments. TS playground can import stuff from npm packages out of the box so you would just need to paste in a bit of code of your schema declaration and drizzleClient - shouldn't be too hard. I'm asking cause I've had colleagues encounter weird behavior with TS in WS so could be an issue on their end this doesn't seem to be a relation query - just a regular one so I don't think that matters here
Aaroned
Aaroned8mo ago
@andreas_444 @Huge Letters sorry, yes I just realised this is not a relation query (I was mislead by the hierarchical structure) If you look at the definition of select you can see if accepts a SelectedFields object, which doesn't allow for nested object hierarchy. Either you can define the query like this:
const res = await drizzleClient
.select({
...getTableColumns(users),
userAbout: {
...getTableColumns(userAbout),
},
avatar: {
...getTableColumns(avatars),
},
photo: {
...getTableColumns(photos),
}
})
.from(users)
.leftJoin(userAbout, eq(userAbout.uid, users.uid))
.leftJoin(avatars, eq(avatars.phid, userAbout.avatarPhid))
.leftJoin(photos, eq(photos.phid, avatars.phid))
.where(eq(users.uid, 2))
const res = await drizzleClient
.select({
...getTableColumns(users),
userAbout: {
...getTableColumns(userAbout),
},
avatar: {
...getTableColumns(avatars),
},
photo: {
...getTableColumns(photos),
}
})
.from(users)
.leftJoin(userAbout, eq(userAbout.uid, users.uid))
.leftJoin(avatars, eq(avatars.phid, userAbout.avatarPhid))
.leftJoin(photos, eq(photos.phid, avatars.phid))
.where(eq(users.uid, 2))
Or define the relation and use a relation query with a full hierarchical data object result.
Huge Letters
Huge Letters8mo ago
yup, tested myself - you're correct, it only allows 1-level deep nesting basically
andreas_444
andreas_444OP8mo ago
I`ve already tried like this
photo: getTableColumns(photos)
photo: getTableColumns(photos)
The same issues In zed this issue appears, too @Aaroned Does the team have any plans to improve the typification for hierarchical queries? Not quite. When typification shows error, the query returns the correct result, regardless of the hierarchy level
Huge Letters
Huge Letters8mo ago
in that case I think you're correct and that's an error with type definition you could file an issue on github but I don't know if this is gonna be a priority - you could fix the select easily like this but it won't infer result type correctly and fixing that seems to be much more complicated, at least at the first glance for me
// /node_modules/drizzle-orm/operations.d.ts

// from
export type SelectedFieldsFlat<TColumn extends Column> = Record<string, TColumn | SQL | SQL.Aliased>;
// to
export type SelectedFieldsFlat<TColumn extends Column> = Record<string, TColumn | SQL | SQL.Aliased | SelectedFieldsFlat<TColumn>>;
// /node_modules/drizzle-orm/operations.d.ts

// from
export type SelectedFieldsFlat<TColumn extends Column> = Record<string, TColumn | SQL | SQL.Aliased>;
// to
export type SelectedFieldsFlat<TColumn extends Column> = Record<string, TColumn | SQL | SQL.Aliased | SelectedFieldsFlat<TColumn>>;
andreas_444
andreas_444OP8mo ago
GitHub
[BUG]: Nested object in select query · Issue #2050 · drizzle-team/d...
What version of drizzle-orm are you using? 0.29.3 What version of drizzle-kit are you using? 0.20.14 Describe the Bug My code is const feedbacks = await drizzleClient .select({ ...getTableColumns(f...
Aaroned
Aaroned8mo ago
FYI I just noticed this comment here https://orm.drizzle.team/docs/rqb#foreign-keys
What this means is relations and foreign keys can be used together, but they are not dependent on each other. You can define relations without using foreign keys (and vice versa), which allows them to be used with databases that do not support foreign keys.
andreas_444
andreas_444OP8mo ago
In my case, I don’t use foreign keys to simplify data mutation in the database. Foreign keys are needed for data consistency. I don`t really need it
Aaroned
Aaroned8mo ago
That's fair enough. But if you want you are welcome to define Drizzle ORM level relations (and hence use relation queries) without having to define database level foreign keys 😊
Want results from more Discord servers?
Add your server