Error: Cannot access 'interactionsRelations' before initialization

My setup is as follows:
// /lib/db/relations.ts
export * from "~/lib/api/users/users.relations.schema";
export * from "~/lib/api/posts/posts.relations.schema";
export * from "~/lib/api/interactions/interactions.relations.schema";

// /lib/db/schema.ts
export * from "~/lib/api/users/users.schema";
export * from "~/lib/api/posts/posts.schema";
export * from "~/lib/api/interactions/interactions.schema";

// /lib/db/index.ts
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

import * as relations from "~/lib/db/relations";
import * as schema from "~/lib/db/schema";

import config from "~/config/db";

const queryClient = postgres(config.dbCredentials.url);

export const db = drizzle(queryClient, { schema: { ...schema, ...relations } });
// /lib/db/relations.ts
export * from "~/lib/api/users/users.relations.schema";
export * from "~/lib/api/posts/posts.relations.schema";
export * from "~/lib/api/interactions/interactions.relations.schema";

// /lib/db/schema.ts
export * from "~/lib/api/users/users.schema";
export * from "~/lib/api/posts/posts.schema";
export * from "~/lib/api/interactions/interactions.schema";

// /lib/db/index.ts
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

import * as relations from "~/lib/db/relations";
import * as schema from "~/lib/db/schema";

import config from "~/config/db";

const queryClient = postgres(config.dbCredentials.url);

export const db = drizzle(queryClient, { schema: { ...schema, ...relations } });
3 Replies
r9d7
r9d7OP3mo ago
A .relation.schema file can reference .schema files like so:
import { relations } from "drizzle-orm";

import { creatorsToPosts, interactions, posts, users } from "~/lib/db/schema";

export const usersRelations = relations(users, ({ many }) => ({
creatorsToPosts: many(creatorsToPosts, { relationName: "creatorsToPosts" }),
interactions: many(interactions),
}));

export const creatorsToPostsRelations = relations(
creatorsToPosts,
({ one }) => ({
creator: one(users, {
fields: [creatorsToPosts.creatorId],
references: [users.id],
relationName: "creatorsToPosts",
}),
post: one(posts, {
fields: [creatorsToPosts.postPublicId],
references: [posts.publicId],
relationName: "postsToCreators",
}),
}),
);
import { relations } from "drizzle-orm";

import { creatorsToPosts, interactions, posts, users } from "~/lib/db/schema";

export const usersRelations = relations(users, ({ many }) => ({
creatorsToPosts: many(creatorsToPosts, { relationName: "creatorsToPosts" }),
interactions: many(interactions),
}));

export const creatorsToPostsRelations = relations(
creatorsToPosts,
({ one }) => ({
creator: one(users, {
fields: [creatorsToPosts.creatorId],
references: [users.id],
relationName: "creatorsToPosts",
}),
post: one(posts, {
fields: [creatorsToPosts.postPublicId],
references: [posts.publicId],
relationName: "postsToCreators",
}),
}),
);
But I never import a .relation.schema file from a .schema file, only the other way around And this is the error I get when I run pnpm db:push:
> @ws0/[email protected] db:push /Users/.../monorepo/apps/web
> drizzle-kit push --config=src/config/db.ts

Reading config file '/Users/.../monorepo/apps/web/src/config/db.ts'
Using 'postgres' driver for database querying
[✓] Pulling schema from database...
ReferenceError: Cannot access 'interactionsRelations' before initialization
at Object.interactionsRelations (/Users/.../monorepo/apps/web/src/lib/api/interactions/interactions.relations.schema.ts:1:1)
at Object.get [as interactionsRelations] (/Users/.../monorepo/apps/web/src/lib/api/interactions/interactions.relations.schema.ts:14:45)
at Object.get [as interactionsRelations] (/Users/.../monorepo/apps/web/src/lib/db/relations.ts:10:45)
at Object.get [as interactionsRelations] (/Users/.../monorepo/apps/web/src/lib/db/index.ts:16:45)
at Object.<anonymous> (/Users/.../monorepo/apps/web/src/lib/db/index.ts:11:66)
at Module._compile (node:internal/modules/cjs/loader:1469:14)
at Module._compile (/Users/.../monorepo/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:14679:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
at Object.newLoader [as .ts] (/Users/.../monorepo/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:14683:13)
at Module.load (node:internal/modules/cjs/loader:1288:32)
> @ws0/[email protected] db:push /Users/.../monorepo/apps/web
> drizzle-kit push --config=src/config/db.ts

Reading config file '/Users/.../monorepo/apps/web/src/config/db.ts'
Using 'postgres' driver for database querying
[✓] Pulling schema from database...
ReferenceError: Cannot access 'interactionsRelations' before initialization
at Object.interactionsRelations (/Users/.../monorepo/apps/web/src/lib/api/interactions/interactions.relations.schema.ts:1:1)
at Object.get [as interactionsRelations] (/Users/.../monorepo/apps/web/src/lib/api/interactions/interactions.relations.schema.ts:14:45)
at Object.get [as interactionsRelations] (/Users/.../monorepo/apps/web/src/lib/db/relations.ts:10:45)
at Object.get [as interactionsRelations] (/Users/.../monorepo/apps/web/src/lib/db/index.ts:16:45)
at Object.<anonymous> (/Users/.../monorepo/apps/web/src/lib/db/index.ts:11:66)
at Module._compile (node:internal/modules/cjs/loader:1469:14)
at Module._compile (/Users/.../monorepo/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:14679:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
at Object.newLoader [as .ts] (/Users/.../monorepo/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:14683:13)
at Module.load (node:internal/modules/cjs/loader:1288:32)
megamindat
megamindat3mo ago
In my case, it happened with circular references. Try moving your schema into one file, that solved the issue.
r9d7
r9d7OP3mo ago
I tried that with no success 😦 This is the most likely cause but I tested my code with madge but it didn't find any circular dependency What ended up working for me was moving every table & relation definition to one schema.ts file, and making sure to export var everything I tried running pnpm db:push with both export const and export let for every table & relation but those would always crash Not the best solution for my project but I just finished refactoring so the app doesn't crash anymore...
Want results from more Discord servers?
Add your server