Separate file for relations
is it possible to define orm relations in a file different from where the tables are defined? i have
users.ts
and relations.ts
, but even if i import "relations";
before instantiating the drizzle object, i keep running into undefined is not an object (evaluating relation.referencedTable)
4 Replies
if i copy the exact same code into
users.ts
it works finewhy would you wanna do that? keep them colocated in the same file itself so its easier to read.
it wont be a problem until you have >1000 lines of code (loc's) & 20-30 tables.
but yes it should be technically possible.
since relations are the only one that refer tables, you can import all tables in
relations.ts
& then try.
but this might be a circular dependency error which sometimes happens when one file is referencing other file & that other file is referencing that file.
even tho it shouldnt happen in this case.
but i think ur prematurely optimizing it right now.
just use 1 file for everything.i do have 20-30 tables- i tried importing the tables to
relations.ts
but i get the undefined error
return type of db.query.users.findFirst({ with: profile })
when it's colocated shows the user profile type
when the relations
call is made elsewhere (e.g. separate relations.ts
file), the type resolution shows { [...other user field types], profile: never }
maybe make a minimal reproduction on stackblitz so others can chime in & help.
just use 2 tables to prove a point.
idk technically it should work.