Skip to content

Commit

Permalink
fix: add relations
Browse files Browse the repository at this point in the history
  • Loading branch information
ephraimduncan committed Jun 8, 2024
1 parent 65a5d08 commit 22fa60a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/db/drizzle/0001_nostalgic_marvel_apes.sql
Original file line number Diff line number Diff line change
@@ -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 $$;
6 changes: 5 additions & 1 deletion packages/db/schema/form-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
9 changes: 7 additions & 2 deletions packages/db/schema/forms.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
6 changes: 5 additions & 1 deletion packages/db/schema/onboarding-forms.ts
Original file line number Diff line number Diff line change
@@ -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(),
});

0 comments on commit 22fa60a

Please sign in to comment.