fix(db-postgres): long array field table aliases cause error even when dbName is used (#11995)

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

Previously, this configuration was causing errors in postgres due to
long names, even though `dbName` is used:
```
{
  slug: 'aliases',
  fields: [
    {
      name: 'thisIsALongFieldNameThatWillCauseAPostgresErrorEvenThoughWeSetAShorterDBName',
      dbName: 'shortname',
      type: 'array',
      fields: [
        {
          name: 'nested_field_1',
          type: 'array',
          dbName: 'short_nested_1',
          fields: [],
        },
        {
          name: 'nested_field_2',
          type: 'text',
        },
      ],
    },
  ],
},
```

This is because we were generating Drizzle relation name (for arrays)
always based on the field path and internally, drizzle uses this name
for aliasing. Now, if `dbName` is present, we use `_{dbName}` instead
for the relation name.
This commit is contained in:
Sasha
2025-04-07 23:12:43 +03:00
committed by GitHub
parent b270901fa6
commit 09782be0e0
10 changed files with 144 additions and 3 deletions

View File

@@ -604,6 +604,29 @@ export default buildConfigWithDefaults({
},
],
},
{
slug: 'aliases',
fields: [
{
name: 'thisIsALongFieldNameThatCanCauseAPostgresErrorEvenThoughWeSetAShorterDBName',
dbName: 'shortname',
type: 'array',
fields: [
{
name: 'nestedArray',
type: 'array',
dbName: 'short_nested_1',
fields: [
{
type: 'text',
name: 'text',
},
],
},
],
},
],
},
],
globals: [
{

View File

@@ -21,6 +21,7 @@ import {
killTransaction,
QueryError,
} from 'payload'
import { assert } from 'ts-essentials'
import { fileURLToPath } from 'url'
import type { Global2 } from './payload-types.js'
@@ -783,6 +784,28 @@ describe('database', () => {
expect(doc.blocks[0].text).toStrictEqual('hello')
expect(doc.blocks[0].localizedText).toStrictEqual('goodbye')
})
it('arrays should work with both long field names and dbName', async () => {
const { id } = await payload.create({
collection: 'aliases',
data: {
thisIsALongFieldNameThatCanCauseAPostgresErrorEvenThoughWeSetAShorterDBName: [
{
nestedArray: [{ text: 'some-text' }],
},
],
},
})
const res = await payload.findByID({ collection: 'aliases', id })
expect(
res.thisIsALongFieldNameThatCanCauseAPostgresErrorEvenThoughWeSetAShorterDBName,
).toHaveLength(1)
const item =
res.thisIsALongFieldNameThatCanCauseAPostgresErrorEvenThoughWeSetAShorterDBName?.[0]
assert(item)
expect(item.nestedArray).toHaveLength(1)
expect(item.nestedArray?.[0]?.text).toBe('some-text')
})
})
describe('transactions', () => {

View File

@@ -80,6 +80,7 @@ export interface Config {
'fake-custom-ids': FakeCustomId;
'relationships-migration': RelationshipsMigration;
'compound-indexes': CompoundIndex;
aliases: Alias;
users: User;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
@@ -100,6 +101,7 @@ export interface Config {
'fake-custom-ids': FakeCustomIdsSelect<false> | FakeCustomIdsSelect<true>;
'relationships-migration': RelationshipsMigrationSelect<false> | RelationshipsMigrationSelect<true>;
'compound-indexes': CompoundIndexesSelect<false> | CompoundIndexesSelect<true>;
aliases: AliasesSelect<false> | AliasesSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
@@ -419,6 +421,26 @@ export interface CompoundIndex {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "aliases".
*/
export interface Alias {
id: string;
thisIsALongFieldNameThatCanCauseAPostgresErrorEvenThoughWeSetAShorterDBName?:
| {
nestedArray?:
| {
text?: string | null;
id?: string | null;
}[]
| null;
id?: string | null;
}[]
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users".
@@ -495,6 +517,10 @@ export interface PayloadLockedDocument {
relationTo: 'compound-indexes';
value: string | CompoundIndex;
} | null)
| ({
relationTo: 'aliases';
value: string | Alias;
} | null)
| ({
relationTo: 'users';
value: string | User;
@@ -795,6 +821,25 @@ export interface CompoundIndexesSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "aliases_select".
*/
export interface AliasesSelect<T extends boolean = true> {
thisIsALongFieldNameThatCanCauseAPostgresErrorEvenThoughWeSetAShorterDBName?:
| T
| {
nestedArray?:
| T
| {
text?: T;
id?: T;
};
id?: T;
};
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users_select".