From 22fa60a9840f8430ad020ff92e1499eef3c60ce8 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Sat, 8 Jun 2024 14:08:43 +0000 Subject: [PATCH] fix: add relations --- .../db/drizzle/0001_nostalgic_marvel_apes.sql | 17 +++++++++++++++++ packages/db/schema/form-data.ts | 6 +++++- packages/db/schema/forms.ts | 9 +++++++-- packages/db/schema/onboarding-forms.ts | 6 +++++- 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 packages/db/drizzle/0001_nostalgic_marvel_apes.sql diff --git a/packages/db/drizzle/0001_nostalgic_marvel_apes.sql b/packages/db/drizzle/0001_nostalgic_marvel_apes.sql new file mode 100644 index 0000000..2950d6b --- /dev/null +++ b/packages/db/drizzle/0001_nostalgic_marvel_apes.sql @@ -0,0 +1,17 @@ +DO $$ BEGIN + ALTER TABLE "form_datas" ADD CONSTRAINT "form_datas_form_id_forms_id_fk" FOREIGN KEY ("form_id") REFERENCES "public"."forms"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "forms" ADD CONSTRAINT "forms_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "onboarding_forms" ADD CONSTRAINT "onboarding_forms_form_id_forms_id_fk" FOREIGN KEY ("form_id") REFERENCES "public"."forms"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; diff --git a/packages/db/schema/form-data.ts b/packages/db/schema/form-data.ts index 265ea37..b4317fe 100644 --- a/packages/db/schema/form-data.ts +++ b/packages/db/schema/form-data.ts @@ -2,11 +2,15 @@ import { index, json, pgTable, text, timestamp } from 'drizzle-orm/pg-core'; import { createInsertSchema, createSelectSchema } from 'drizzle-zod'; import { type z } from 'zod'; +import { forms } from './forms'; + export const formDatas = pgTable( 'form_datas', { id: text('id').primaryKey(), - formId: text('form_id').notNull(), + formId: text('form_id') + .references(() => forms.id, { onDelete: 'cascade' }) + .notNull(), data: json('data').notNull(), createdAt: timestamp('created_at').defaultNow().notNull(), }, diff --git a/packages/db/schema/forms.ts b/packages/db/schema/forms.ts index f87979b..681246e 100644 --- a/packages/db/schema/forms.ts +++ b/packages/db/schema/forms.ts @@ -1,12 +1,17 @@ -import { type InferSelectModel } from 'drizzle-orm'; +import type { InferSelectModel } from 'drizzle-orm'; + import { boolean, index, pgTable, text, timestamp } from 'drizzle-orm/pg-core'; import { createInsertSchema, createSelectSchema } from 'drizzle-zod'; +import { users } from './users'; + export const forms = pgTable( 'forms', { id: text('id').primaryKey(), - userId: text('user_id').notNull(), + userId: text('user_id') + .references(() => users.id, { onDelete: 'cascade' }) + .notNull(), title: text('title').notNull(), description: text('description'), createdAt: timestamp('created_at').defaultNow().notNull(), diff --git a/packages/db/schema/onboarding-forms.ts b/packages/db/schema/onboarding-forms.ts index 54b3c5f..5118e0c 100644 --- a/packages/db/schema/onboarding-forms.ts +++ b/packages/db/schema/onboarding-forms.ts @@ -1,8 +1,12 @@ import { pgTable, text, timestamp } from 'drizzle-orm/pg-core'; +import { forms } from './forms'; + export const onboardingForms = pgTable('onboarding_forms', { id: text('id').primaryKey(), userId: text('user_id').notNull(), - formId: text('form_id').notNull(), + formId: text('form_id') + .references(() => forms.id, { onDelete: 'cascade' }) + .notNull(), createdAt: timestamp('created_at').defaultNow().notNull(), });