diff --git a/core/collection_model.go b/core/collection_model.go index 93b67fe2..107dc194 100644 --- a/core/collection_model.go +++ b/core/collection_model.go @@ -28,6 +28,8 @@ const ( const systemHookIdCollection = "__pbCollectionSystemHook__" +const defaultLowercaseRecordIdPattern = "^[a-z0-9]+$" + func (app *BaseApp) registerCollectionHooks() { app.OnModelValidate().Bind(&hook.Handler[*ModelEvent]{ Id: systemHookIdCollection, @@ -890,7 +892,7 @@ func (c *Collection) initIdField() { Required: true, Min: 15, Max: 15, - Pattern: `^[a-z0-9]+$`, + Pattern: defaultLowercaseRecordIdPattern, AutogeneratePattern: `[a-z0-9]{15}`, } @@ -903,7 +905,7 @@ func (c *Collection) initIdField() { field.PrimaryKey = true field.Hidden = false if field.Pattern == "" { - field.Pattern = `^[a-z0-9]+$` + field.Pattern = defaultLowercaseRecordIdPattern } } } diff --git a/core/field_text.go b/core/field_text.go index 87c29e1f..e86c9946 100644 --- a/core/field_text.go +++ b/core/field_text.go @@ -192,15 +192,17 @@ func (f *TextField) ValidateValue(ctx context.Context, app App, record *Record) // side-effects on some platforms check for duplicates in a case-insensitive manner // // (@todo eventually may get replaced in the future with a system unique constraint to avoid races or wrapping the request in a transaction) - var exists bool - err := app.DB(). - Select("(1)"). - From(record.TableName()). - Where(dbx.NewExp("id = {:id} COLLATE NOCASE", dbx.Params{"id": strings.ToLower(newVal)})). - Limit(1). - Row(&exists) - if exists || (err != nil && !errors.Is(err, sql.ErrNoRows)) { - return validation.NewError("validation_pk_invalid", "The record primary key is invalid or already exists.") + if f.Pattern != defaultLowercaseRecordIdPattern { + var exists bool + err := app.DB(). + Select("(1)"). + From(record.TableName()). + Where(dbx.NewExp("id = {:id} COLLATE NOCASE", dbx.Params{"id": strings.ToLower(newVal)})). + Limit(1). + Row(&exists) + if exists || (err != nil && !errors.Is(err, sql.ErrNoRows)) { + return validation.NewError("validation_pk_invalid", "The record primary key is invalid or already exists.") + } } } }