pocketbase / pocketbase

Open Source realtime backend in 1 file
https://pocketbase.io
MIT License
36.06k stars 1.62k forks source link

Bug: Collections with indexes created using sdk does not behave properly #5118

Closed daxomy closed 1 week ago

daxomy commented 1 week ago

https://github.com/pocketbase/pocketbase/assets/20862517/db9ef546-ee78-4035-8a28-28b9fff1fec4

Reproduced on v0.22.14

import PocketBase from 'pocketbase'
const pb = new PocketBase('http://localhost:8090')
await pb.admins.authWithPassword('test@test.test', '1111111111')
pb.autoCancellation(false)

for (let index = 0; index < 10; index++) {
    const name = generateRandomString(10)
    const index = "CREATE UNIQUE INDEX" + " " + "`" + "time_index" + "`" + " " + "ON" + " " + "`" + name + "`" + " " + "(`time`)"
    await pb.collections.create({
        name: name,
        indexes: [index],
        schema: [
            { name: "time", type: "number" }
        ]
    })
}

function generateRandomString(length) {
    let result = '';
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return result;
}
ganigeorgiev commented 1 week ago

As far as I understand from the video the problem seems related to the unique constraint but note that the unique constraint is handled on SQLite level.

In your case the issue seems to be related to the fact that the index is created with the same name for each collection, aka. the last saved collection will most likely delete the previous index. Or in other words - use a unique index name.

I'll ~consider adding~ (done) a validator with the refactored Collection model for this use case to prevent duplicated collection index names.