fix: Detach agents before initiative deletion to prevent FK constraint failure
Nulls out agents.initiativeId before deleting the initiative row, ensuring the delete succeeds even on databases where migration 0025 (which adds ON DELETE SET NULL to the FK) hasn't been applied.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { nanoid } from 'nanoid';
|
||||
import type { DrizzleDatabase } from '../../index.js';
|
||||
import { initiatives, type Initiative } from '../../schema.js';
|
||||
import { agents, initiatives, type Initiative } from '../../schema.js';
|
||||
import type {
|
||||
InitiativeRepository,
|
||||
CreateInitiativeData,
|
||||
@@ -74,6 +74,10 @@ export class DrizzleInitiativeRepository implements InitiativeRepository {
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
// Detach agents before deleting — agents.initiative_id FK may lack ON DELETE SET NULL
|
||||
// in databases that haven't applied migration 0025 yet.
|
||||
await this.db.update(agents).set({ initiativeId: null }).where(eq(agents.initiativeId, id));
|
||||
|
||||
const [deleted] = await this.db.delete(initiatives).where(eq(initiatives.id, id)).returning();
|
||||
|
||||
if (!deleted) {
|
||||
|
||||
Reference in New Issue
Block a user