From 0040e078d8bd58f2acba28bba8d866eabf43eaec Mon Sep 17 00:00:00 2001 From: Chris Parsons Date: Fri, 1 May 2026 21:19:14 +0000 Subject: [PATCH] schema: add check constraint requiring tournament_id for qualifying events Team sports (NHL, NBA, etc.) have scoring_events with no canonical tournament, so we can't require tournament_id globally. The constraint only applies when is_qualifying_event=true. Phase 3 Task 1 of canonical tournament layer migration. Co-Authored-By: Claude Opus 4.7 --- database/schema.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/database/schema.ts b/database/schema.ts index f763fca..bff5ea5 100644 --- a/database/schema.ts +++ b/database/schema.ts @@ -1,4 +1,4 @@ -import { integer, pgTable, varchar, uuid, timestamp, pgEnum, boolean, text, date, decimal, uniqueIndex, index, jsonb } from "drizzle-orm/pg-core"; +import { check, integer, pgTable, varchar, uuid, timestamp, pgEnum, boolean, text, date, decimal, uniqueIndex, index, jsonb } from "drizzle-orm/pg-core"; import { relations, sql } from "drizzle-orm"; export const users = pgTable("users", { @@ -554,7 +554,12 @@ export const scoringEvents = pgTable("scoring_events", { completedAt: timestamp("completed_at"), createdAt: timestamp("created_at").defaultNow().notNull(), updatedAt: timestamp("updated_at").defaultNow().notNull(), -}); +}, (t) => ({ + qualifyingEventsRequireTournament: check( + "scoring_events_qualifying_require_tournament", + sql`NOT ${t.isQualifyingEvent} OR ${t.tournamentId} IS NOT NULL`, + ), +})); // Results for participants in specific events export const eventResults = pgTable("event_results", {