From 336fc43b241a7161e0899d36981c2cb33de2e01f Mon Sep 17 00:00:00 2001 From: Bentlybro Date: Mon, 19 Jan 2026 13:39:20 +0000 Subject: [PATCH] Add unique constraint to LlmModelCost on model, provider, unit Introduces a unique index on the combination of llmModelId, credentialProvider, and unit in the LlmModelCost table to prevent duplicate cost entries. Updates the seed migration to handle conflicts on this unique key by doing nothing on conflict. --- .../migrations/20251126113000_add_llm_registry/migration.sql | 3 +++ .../migrations/20251126120000_seed_llm_registry/migration.sql | 3 ++- autogpt_platform/backend/schema.prisma | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/autogpt_platform/backend/migrations/20251126113000_add_llm_registry/migration.sql b/autogpt_platform/backend/migrations/20251126113000_add_llm_registry/migration.sql index 704bc17486..017c4244d6 100644 --- a/autogpt_platform/backend/migrations/20251126113000_add_llm_registry/migration.sql +++ b/autogpt_platform/backend/migrations/20251126113000_add_llm_registry/migration.sql @@ -70,6 +70,9 @@ CREATE INDEX "LlmModelCost_llmModelId_idx" ON "LlmModelCost"("llmModelId"); -- CreateIndex CREATE INDEX "LlmModelCost_credentialProvider_idx" ON "LlmModelCost"("credentialProvider"); +-- CreateIndex +CREATE UNIQUE INDEX "LlmModelCost_llmModelId_credentialProvider_unit_key" ON "LlmModelCost"("llmModelId", "credentialProvider", "unit"); + -- AddForeignKey ALTER TABLE "LlmModel" ADD CONSTRAINT "LlmModel_providerId_fkey" FOREIGN KEY ("providerId") REFERENCES "LlmProvider"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/autogpt_platform/backend/migrations/20251126120000_seed_llm_registry/migration.sql b/autogpt_platform/backend/migrations/20251126120000_seed_llm_registry/migration.sql index a59ee716fa..2163d533bf 100644 --- a/autogpt_platform/backend/migrations/20251126120000_seed_llm_registry/migration.sql +++ b/autogpt_platform/backend/migrations/20251126120000_seed_llm_registry/migration.sql @@ -221,5 +221,6 @@ FROM (VALUES ('v0-1.0-md', 1) ) AS costs(model_slug, cost) JOIN model_ids m ON m."slug" = costs.model_slug -JOIN provider_ids p ON p."id" = m."providerId"; +JOIN provider_ids p ON p."id" = m."providerId" +ON CONFLICT ("llmModelId", "credentialProvider", "unit") DO NOTHING; diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma index 3a248320c2..84b101a1dd 100644 --- a/autogpt_platform/backend/schema.prisma +++ b/autogpt_platform/backend/schema.prisma @@ -1198,6 +1198,7 @@ model LlmModelCost { llmModelId String Model LlmModel @relation(fields: [llmModelId], references: [id], onDelete: Cascade) + @@unique([llmModelId, credentialProvider, unit]) @@index([llmModelId]) @@index([credentialProvider]) }