From bd1daedf898eb761d91ae50e84b60f2d125252ba Mon Sep 17 00:00:00 2001 From: melvinchia3636 Date: Wed, 28 Jan 2026 12:33:57 +0800 Subject: [PATCH] fix: prevent duplicate indexes from being added to collections during migration generation. --- .../generateContent/structure.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/src/commands/db/functions/migration-generation/generateContent/structure.ts b/tools/src/commands/db/functions/migration-generation/generateContent/structure.ts index 383d14810..436e0431b 100644 --- a/tools/src/commands/db/functions/migration-generation/generateContent/structure.ts +++ b/tools/src/commands/db/functions/migration-generation/generateContent/structure.ts @@ -22,21 +22,30 @@ async function mapCollectionRelation( const mapped = { ...raw } - const realCollectionId = allCollectionsInPB.find( + const realCollection = allCollectionsInPB.find( collection => collection.name === raw.name - )?.id + ) - if (!realCollectionId) { + if (!realCollection?.id) { throw new Error( `Collection "${raw.name}" not found in PocketBase for relation field "${raw.name}"` ) } - mapped.id = realCollectionId + mapped.id = realCollection.id delete mapped.created delete mapped.updated + for (const index of mapped.indexes) { + const found = realCollection.indexes.find(idx => idx === index) + + // If the index is already in the collection, we don't need to add it again + if (found) { + mapped.indexes = mapped.indexes.filter(idx => idx !== index) + } + } + if (mapped.fields && Array.isArray(mapped.fields)) { mapped.fields = mapped.fields.map(field => { const cleanedField = { ...field }