Nested Relations Where has Zero Typings :confused:

Hey guys - first post so please let me know if this isn't the wrong place or I'm doing something wrong. Anyhow, I'm trying to order a simple nested relation in a query:
await db.query.channel.findMany({
with: {
pins: {
orderBy: (pin, { desc }) => [desc(pin.createdAt)]
}
}
});
await db.query.channel.findMany({
with: {
pins: {
orderBy: (pin, { desc }) => [desc(pin.createdAt)]
}
}
});
Now - the with has no typings at all, so I have to hand craft the query with zero type annotations. There's also an issue of the desc binding element being implicitly of type any also, but that's for another day. Has anybody come across this, and if so - is there a fix.. Or, more likely - am I doing something wrong, that isn't meant to be done this way?
No description
9 Replies
Luxaritas
Luxaritas17mo ago
If you’re using relations(), have you exported them? Are all your tables using district names? (Those are two reasons I’ve seen for that)
megmut
megmutOP17mo ago
Here's my relation:
export const channelRelations = relations(channel, ({ one, many }) => ({
category: one(category, {
fields: [channel.categoryId],
references: [category.id]
}),
pins: many(pin)
}));
export const channelRelations = relations(channel, ({ one, many }) => ({
category: one(category, {
fields: [channel.categoryId],
references: [category.id]
}),
pins: many(pin)
}));
Regarding the exporting - I think so yes:
- schema.ts
export { channel, channelRelations } from './tables/channel';
export { PinType, pin, pinRelations } from './tables/pin';
- schema.ts
export { channel, channelRelations } from './tables/channel';
export { PinType, pin, pinRelations } from './tables/pin';
Luxaritas
Luxaritas17mo ago
And when you pass schema to the drizzle constructor, it includes those, right?
megmut
megmutOP17mo ago
It would appear so, yes
No description
megmut
megmutOP17mo ago
And used like so: export const db = drizzle(sql, { schema });
Luxaritas
Luxaritas17mo ago
Hm from what you’ve shown it looks correct. My assumption is that there’s something with the schema not configured correctly but I don’t see it here. Again I’d double check that all tables have unique names passed as the first argument, but otherwise I don’t have much to go off here. I might suggest trying to put together a minimum reproduction, which may lead to building up to what the issue is - if you do it in stackblitz or such I could even poke at it myself
megmut
megmutOP17mo ago
I'm rebuilding a single schema file to try that now. Wondering if splitting each table and relations into it's own file isn't helping matters. If no joy though, I'll go create an example on stackblitz! Cheers for the help so far, will report back when I have something
Luxaritas
Luxaritas17mo ago
Single file shouldnt matter as long as it winds up directly on the top level of the schema object But easier to debug at least
megmut
megmutOP17mo ago
yeah, single file didn't help 😦 Will make stackblitz tomorrow 🥱 Hey man - soo.. slightly frustrating: https://stackblitz.com/edit/stackblitz-starters-thhb1m?file=src%2Fexample.ts However, it works 100% fine 😦 Even the type intersection so.. even though the versions are the same for both orm and kit.. the typings are just nuked on my project I am using sveltekit and a few other bits of tech, so maybe that's the issue as the tsconfig extends sveltekit one and other stuff 😢 Anyway - will report here once I've found the root cause in case anybody else ever finds this Eughhhhhh So I finally debugged the issue and it turned out to be the dumbest thing ever 😂 Another relation I had declared was referencing the wrong table..
// this was the error
export const tableARelations = relations(tableB, ({ many }) => ({
items: many(tableB),
}));

// when it should have been:

export const tableARelations = relations(tableA, ({ many }) => ({
items: many(tableB),
}));
// this was the error
export const tableARelations = relations(tableB, ({ many }) => ({
items: many(tableB),
}));

// when it should have been:

export const tableARelations = relations(tableA, ({ many }) => ({
items: many(tableB),
}));
Boy do I feel dumb now - what a nightmare trying to debug all that typescript 😂 Anyway, if anybody happens to come across the same thing, please check the relations closely as it's very easy to miss them when the tables are named similarly
Want results from more Discord servers?
Add your server