Circular Dependency Hell

Is it just me or does using the relations API result in a bunch of circular deps that aren't lazily instantiated. For example, a foreign key reference is a function.
workspaceId: cuid2('workspace_id')
.notNull()
.references(() => workspace.id)
workspaceId: cuid2('workspace_id')
.notNull()
.references(() => workspace.id)
For relations, it's something like this: workspace: one(workspace),. I feel like everything needs to be lazily instantiated because I'm eating alive by: Cannot read properties of undefined (reading 'Symbol(drizzle:Name)'). I've spent 20-30 hours fighting it and I think I'm just going to delete the relations API although even though I like the DX.
6 Replies
Marcus
Marcus5mo ago
I guess it's just me, sad!
MAST
MAST5mo ago
Oh, I think you need to alias the workspace and then reference it. For example if the table is called workspaces you can do this:
const workspaces = pgTable('workspaces', {
workspaceId: cuid2('workspace_id')
.notNull()
.references(() => aliasedWorkspace.id),
});
const aliasedWorkspace = alias(workspaces, "aliased_workspace");
const workspaces = pgTable('workspaces', {
workspaceId: cuid2('workspace_id')
.notNull()
.references(() => aliasedWorkspace.id),
});
const aliasedWorkspace = alias(workspaces, "aliased_workspace");
I remember seeing this in the old docs.
MAST
MAST5mo ago
Oh, found this in the new docs:
No description
MAST
MAST5mo ago
This should probably fix your problem.
MAST
MAST5mo ago
Drizzle ORM - Indexes & Constraints
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Marcus
Marcus5mo ago
I have that for types on a self referencing type. My issue is a runtime error when two different models reference each other. Since there no closure on relations the value is undefined since it’s hoisted to the top on compilation