fix: prevent duplicate indexes from being added to collections during migration generation.

This commit is contained in:
melvinchia3636
2026-01-28 12:33:57 +08:00
parent 9b222aad23
commit bd1daedf89

View File

@@ -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 }