add relationships to schema

This commit is contained in:
Nayan Sawyer
2026-03-15 09:54:51 -04:00
parent 7818db770f
commit bd8a5a5602
4 changed files with 1484 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
CREATE TABLE "relationship_types" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" text NOT NULL,
"group" text,
"user_id" text,
"updated_at" timestamp with time zone DEFAULT now(),
"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "relationships" (
"person1_id" uuid,
"person2_id" uuid,
"relationship_type_id" uuid,
CONSTRAINT "relationships_person1_id_person2_id_pk" PRIMARY KEY("person1_id","person2_id")
);
--> statement-breakpoint
ALTER TABLE "relationship_types" ADD CONSTRAINT "relationship_types_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "relationships" ADD CONSTRAINT "relationships_person1_id_person_id_fk" FOREIGN KEY ("person1_id") REFERENCES "public"."person"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "relationships" ADD CONSTRAINT "relationships_person2_id_person_id_fk" FOREIGN KEY ("person2_id") REFERENCES "public"."person"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "relationships" ADD CONSTRAINT "relationships_relationship_type_id_relationship_types_id_fk" FOREIGN KEY ("relationship_type_id") REFERENCES "public"."relationship_types"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_relationships_person1" ON "relationships" USING btree ("person1_id");--> statement-breakpoint
CREATE INDEX "idx_relationships_person2" ON "relationships" USING btree ("person2_id");--> statement-breakpoint
CREATE INDEX "idx_relationships_relationship_type" ON "relationships" USING btree ("relationship_type_id");

File diff suppressed because it is too large Load Diff

View File

@@ -15,6 +15,13 @@
"when": 1773582364110, "when": 1773582364110,
"tag": "0001_famous_karma", "tag": "0001_famous_karma",
"breakpoints": true "breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1773582870525,
"tag": "0002_remarkable_meggan",
"breakpoints": true
} }
] ]
} }

View File

@@ -173,3 +173,38 @@ export const activityReports = pgTable(
index("idx_activity_reports_report").on(table.reportId), index("idx_activity_reports_report").on(table.reportId),
], ],
); );
export const relationshipTypes = pgTable("relationship_types", {
id: uuid("id").defaultRandom().primaryKey(),
name: text("name").notNull(),
group: text("group"),
userId: text("user_id").references(() => user.id, {
onDelete: "cascade",
}),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
});
export const relationships = pgTable(
"relationships",
{
person1Id: uuid("person1_id").references(() => person.id, {
onDelete: "cascade",
}),
person2Id: uuid("person2_id").references(() => person.id, {
onDelete: "cascade",
}),
relationshipTypeId: uuid("relationship_type_id").references(
() => relationshipTypes.id,
{
onDelete: "cascade",
},
),
},
(table) => [
primaryKey({ columns: [table.person1Id, table.person2Id] }),
index("idx_relationships_person1").on(table.person1Id),
index("idx_relationships_person2").on(table.person2Id),
index("idx_relationships_relationship_type").on(table.relationshipTypeId),
],
);