Can't create relation

Hi everyone ! I have 2 files here is the first :
import { pgTable, text } from "drizzle-orm/pg-core";
import { userTable } from "./user";
import { relations } from "drizzle-orm";
import { setupPhotoTable } from "./setup_photo";

export const setupTable = pgTable("setup", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => userTable.id),
name: text("name"),
description: text("description"),
});

export const setupTableRelation = relations(setupPhotoTable, ({ many }) => ({
setupPhotos: many(setupPhotoTable)
}))

export type Setup = typeof setupTable.$inferSelect;
import { pgTable, text } from "drizzle-orm/pg-core";
import { userTable } from "./user";
import { relations } from "drizzle-orm";
import { setupPhotoTable } from "./setup_photo";

export const setupTable = pgTable("setup", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => userTable.id),
name: text("name"),
description: text("description"),
});

export const setupTableRelation = relations(setupPhotoTable, ({ many }) => ({
setupPhotos: many(setupPhotoTable)
}))

export type Setup = typeof setupTable.$inferSelect;
Here is the second :
import { pgTable, text } from "drizzle-orm/pg-core";
import { setupTable } from "./setup";
import { mediaTable } from "./media";
import { relations } from "drizzle-orm";

export const setupPhotoTable = pgTable("setup_photo", {
id: text("id").primaryKey(),
setupId: text("user_id")
.notNull()
.references(() => setupTable.id),
mediaId: text("media_id").notNull().references(() => mediaTable.id),
});

export const setupPhotoTableRelation = relations(setupTable, ({ one }) => ({
setup: one(setupTable),
media: one(mediaTable),
}));

export type SetupPhoto = typeof setupPhotoTable.$inferSelect;
import { pgTable, text } from "drizzle-orm/pg-core";
import { setupTable } from "./setup";
import { mediaTable } from "./media";
import { relations } from "drizzle-orm";

export const setupPhotoTable = pgTable("setup_photo", {
id: text("id").primaryKey(),
setupId: text("user_id")
.notNull()
.references(() => setupTable.id),
mediaId: text("media_id").notNull().references(() => mediaTable.id),
});

export const setupPhotoTableRelation = relations(setupTable, ({ one }) => ({
setup: one(setupTable),
media: one(mediaTable),
}));

export type SetupPhoto = typeof setupPhotoTable.$inferSelect;
But when i start my project, got this error :
No description
2 Replies
Alexandre
AlexandreOP5mo ago
So i don't understand how to properly declare relations, can someone help me ? Thanks a lot ! ❤️
rphlmr ⚡
rphlmr ⚡5mo ago
👋 You have a circular dependency, file A import B and B import A. You need a 3rd file with all your relations or group your tables in one file.
Want results from more Discord servers?
Add your server