r9d7
r9d7
DTDrizzle Team
Created by r9d7 on 9/23/2024 in #help
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 } });
11 replies
DTDrizzle Team
Created by r9d7 on 3/20/2024 in #help
Relations between tables unclear
Consider the following schema where a User can create a Post, and each post can have multiple versions of its content. What is the best way for a Post to store the latestVersionId for quick access. I'm having trouble figuring that out, and also how to create a Post since a Post would need a PostVersion first, but a PostVersion needs a Post to link them together.
export const posts = pgTable("posts", {
id: serial("id").primaryKey(),

authorId: integer("author_id").notNull(),
parentPostId: integer("parent_post_id"),
latestVersionId: integer("latest_version_id") // Is this the best way to store a reference to the latest post_version?
.notNull() // It doesn't make sense for the latestVersionId to be null, so I set it as notNull. But in this case, how can I create a Post or a PostVersion? Either of these need the other during their creation
.references(() => recordVersions.id),
});

export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [records.authorId],
references: [users.id],
}),
parentPost: one(posts, {
fields: [posts.parentPostId],
references: [posts.id],
}),
versions: many(postVersions),
}));

export const postVersions = pgTable("post_versions", {
id: serial("id").primaryKey(),

authorId: integer("author_id").notNull(),
postId: integer("postId").notNull(),

content: jsonb("content"),
});

export const postVersionsRelations = relations(postVersions, ({ one }) => ({
author: one(users, {
fields: [postVersions.authorId],
references: [users.id],
}),
post: one(posts, {
fields: [postVersions.postId],
references: [posts.id],
}),
}));
export const posts = pgTable("posts", {
id: serial("id").primaryKey(),

authorId: integer("author_id").notNull(),
parentPostId: integer("parent_post_id"),
latestVersionId: integer("latest_version_id") // Is this the best way to store a reference to the latest post_version?
.notNull() // It doesn't make sense for the latestVersionId to be null, so I set it as notNull. But in this case, how can I create a Post or a PostVersion? Either of these need the other during their creation
.references(() => recordVersions.id),
});

export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [records.authorId],
references: [users.id],
}),
parentPost: one(posts, {
fields: [posts.parentPostId],
references: [posts.id],
}),
versions: many(postVersions),
}));

export const postVersions = pgTable("post_versions", {
id: serial("id").primaryKey(),

authorId: integer("author_id").notNull(),
postId: integer("postId").notNull(),

content: jsonb("content"),
});

export const postVersionsRelations = relations(postVersions, ({ one }) => ({
author: one(users, {
fields: [postVersions.authorId],
references: [users.id],
}),
post: one(posts, {
fields: [postVersions.postId],
references: [posts.id],
}),
}));
1 replies