| 1 | import { defineRelations } from "drizzle-orm"; |
| 2 | import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; |
| 3 | |
| 4 | export const users = sqliteTable("users", { |
| 5 | id: integer("id").primaryKey({ autoIncrement: true }), |
| 6 | name: text("name").notNull(), |
| 7 | }); |
| 8 | |
| 9 | export const posts = sqliteTable("posts", { |
| 10 | id: integer("id").primaryKey({ autoIncrement: true }), |
| 11 | userId: integer("user_id") |
| 12 | .notNull() |
| 13 | .references(() => users.id, { onDelete: "cascade" }), |
| 14 | title: text("title").notNull(), |
| 15 | }); |
| 16 | |
| 17 | export const relations = defineRelations({ users, posts }, (t) => ({ |
| 18 | users: { |
| 19 | posts: t.many.posts(), |
| 20 | }, |
| 21 | posts: { |
| 22 | author: t.one.users({ |
| 23 | from: t.posts.userId, |
| 24 | to: t.users.id, |
| 25 | }), |
| 26 | }, |
| 27 | })); |