You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that calling relations multiple times for the same table breaks Typescript inference. See code below for what I mean. Having manufacturersRelations1 and manufacturersRelations2 split like that causes the relations to disappear from the types altogether. If you combine it into one call it's alright.
constlocationsRelations=relations(locations,({ many })=>({manufacturers: many(manufacturers),}));constmanufacturersRelations1=relations(manufacturers,({ many })=>({products: many(products),}));constmanufacturersRelations2=relations(manufacturers,({ one })=>({location: one(locations,{fields: [manufacturers.locationId],references: [locations.id],}),}));
constproduct=awaitdb.query.products.findFirst({with: {manufacturer: {with: {location: true,},},},});if(!product){throwError("No product found");}product.manufacturer.location;// ^^^ Property 'location' does not exist on type '{ id: number; locationId: number; }'. Did you mean 'locationId'?(2551)
The reason I want to do this is that I made some utility functions for relations e.g.
exportfunctiononeToMany(aOne: TableWithId,bMany: PgTable,bManyField: PgColumn,): [ReturnType<typeofrelations>,ReturnType<typeofrelations>]{constaName=getTableName(aOne);constbName=getTableName(bMany);// just remove the last letter which should be an 's'constaSingularName=aName.slice(0,-1);constaRelations=relations(aOne,({ many })=>({[bName]: many(bMany),}));constbRelations=relations(bMany,({ one })=>({[aSingularName]: one(aOne,{fields: [bManyField],references: [aOne.id],}),}));return[aRelations,bRelations];}
Where the above relations declarations would become:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I noticed that calling
relations
multiple times for the same table breaks Typescript inference. See code below for what I mean. HavingmanufacturersRelations1
andmanufacturersRelations2
split like that causes the relations to disappear from the types altogether. If you combine it into one call it's alright.full code
full code
Playground Link
The reason I want to do this is that I made some utility functions for relations e.g.
Where the above relations declarations would become:
Playground link
Where as you see, the manufacturers relations need to be split. Is this behaviour expected? Is there a way to make this kind of thing work?
Beta Was this translation helpful? Give feedback.
All reactions