fix(db-sqlite): text field converts to floating point number (#12107)

### What?

Converts numbers passed to a text field to avoid the database/drizzle
from converting it incorrectly.

### Why?

If you have a hook that passes a value to another field you can
experience this problem where drizzle converts a number value for a text
field to a floating point number in sqlite for example.

### How?

Adds logic to `transform/write/traverseFields.ts` to cast text field
values to string.
This commit is contained in:
Dan Ribbens
2025-04-14 17:05:08 -04:00
committed by GitHub
parent da7be35a15
commit 6572bf4ae1
3 changed files with 21 additions and 0 deletions

View File

@@ -44,6 +44,10 @@ export default buildConfigWithDefaults({
type: 'text',
required: true,
},
{
name: 'text',
type: 'text',
},
{
name: 'number',
type: 'number',

View File

@@ -1979,6 +1979,19 @@ describe('database', () => {
})
})
it('should convert numbers to text', async () => {
const result = await payload.create({
collection: postsSlug,
data: {
title: 'testing',
// @ts-expect-error hardcoding a number and expecting that it will convert to string
text: 1,
},
})
expect(result.text).toStrictEqual('1')
})
it('should not allow to query by a field with `virtual: true`', async () => {
await expect(
payload.find({