fix(db-sqlite): sqlite unique validation messages (#12740)

Fixes https://github.com/payloadcms/payload/issues/12628

When using sqlite, the error from the db is a bit different than
Postgres.

This PR allows us to extract the fieldName when using sqlite for the
unique constraint error.
This commit is contained in:
Jarrod Flesch
2025-06-10 10:08:06 -04:00
committed by GitHub
parent 38652d7b7c
commit 254ffecaea
3 changed files with 64 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
import type { MongooseAdapter } from '@payloadcms/db-mongodb'
import type { PostgresAdapter } from '@payloadcms/db-postgres/types'
import type { NextRESTClient } from 'helpers/NextRESTClient.js'
import type { Payload, PayloadRequest, TypeWithID } from 'payload'
import type { Payload, PayloadRequest, TypeWithID, ValidationError } from 'payload'
import {
migrateRelationshipsV2_V3,
@@ -2646,4 +2646,24 @@ describe('database', () => {
expect(docs[1].id).toBe(post_3.id)
expect(docs[2].id).toBe(post_1.id)
})
it('should throw specific unique contraint errors', async () => {
await payload.create({
collection: 'unique-fields',
data: {
slugField: 'unique-text',
},
})
try {
await payload.create({
collection: 'unique-fields',
data: {
slugField: 'unique-text',
},
})
} catch (e) {
expect((e as ValidationError).message).toEqual('The following field is invalid: slugField')
}
})
})