Skip to main content

Relationships

These can be complex, both sides need to know how they are connected and what to do

Many-to-one

Many to one relationships

import "@skibididrizz/drizzle";
using Drizzle;

@table model Blog {
@uuid @id id: string;
name: string;
@map("author_id") authorId: string;
@relation(#{fields:"authorId"}) author: Author;
};

@table model Author {
@uuid @id id: string;
name: string;
blogs:Blog[];
};



import { pgTable,uuid, text } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

export const BlogTable = pgTable("Blog", {
id: uuid("id").primaryKey(),
name: text("name").notNull(),
authorId: text("author_id").notNull(),
});

export type Blog = typeof BlogTable.$inferSelect;
export const BlogTableRelations = relations(BlogTable, ({ one }) => ({
author: one(AuthorTable, {
relationName: "author",
fields: [BlogTable.authorId],
references: [AuthorTable.id],
}),
}));

export const AuthorTable = pgTable("Author", {
id: text("id").primaryKey(),
name: text("name").notNull(),
});

export type Author = typeof AuthorTable.$inferSelect;
export const AuthorTableRelations = relations(AuthorTable, ({ many }) => ({
blogs: many(BlogTable),
}));


Many-to-Many

One-to-One