-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Description
Describe the Bug
When using the @payloadcms/storage-s3 plugin with a prefix configured for a media collection (e.g., media), and deploying to an environment that uses database migrations (like Vercel with Supabase/Postgres, or a local production-like setup), a runtime error column "prefix" does not exist occurs. This error typically happens during operations on the media collection, such as seeding (specifically when attempting to delete existing media).
The issue seems to stem from a discrepancy:
The prefix option in the s3Storage plugin configuration (e.g., collections: { media: { prefix: 'media' } }) leads to the generated payload-types.ts including prefix?: string | null; as an optional field in the Media interface.
However, the database migration files generated by PayloadCMS do not include a prefix column in the media table schema.
When PayloadCMS performs database operations, it appears to rely on the payload-types.ts definition, causing the ORM (db-postgres with Drizzle) to query for a prefix column that doesn't exist in the database schema when migrations are the source of truth.
This bug does not manifest in local development environments when NODE_ENV=development if payload db:push is used, likely because db:push synchronizes the schema (potentially creating the prefix column) based on the (plugin-influenced) payload-types.ts or derived config. However, the error surfaces when NODE_ENV=production or when relying strictly on migrations, as the prefix column is not part of the formal migration-defined schema.
Expected Behavior:
The prefix configuration within the S3 storage plugin should be used solely for determining the storage path in the S3 bucket. It should not:
- Cause a
prefixfield to be added to theMediacollection's type definition inpayload-types.tsunless explicitly defined as a custom field by the user in their collection config. - Lead to the ORM attempting to query a
prefixcolumn in the database if that column is not defined in the migrations.
Link to the code that reproduces this issue
Reproduction Steps
- Configure a new PayloadCMS project with
@payloadcms/db-postgresand@payloadcms/storage-s3. - Define a
mediacollection similar to the one insrc/collections/Media.ts(without aprefixfield). - In
payload.config.ts, add the@payloadcms/storage-s3plugin and configure aprefixfor themediacollection (e.g.,prefix: 'media'). - Generate types:
pnpm run payload generate:types. Observe thatpayload-types.tsnow includesprefix?: string | null;in theMediainterface. - Create an initial migration:
pnpm run payload migrate:create initial_schema. Inspect the migration SQL; it will not contain aprefixcolumn for themediatable. - Run the migration against a clean Postgres database:
pnpm run payload migrate. - Set
NODE_ENV=production. - Attempt to seed the database or perform operations that involve querying or deleting from the
mediacollection (e.g., using a seed script that clears collections, like the one in yoursrc/endpoints/seed/index.tswhich callspayload.db.deleteMany({ collection: 'media', ... })). - Observe the error:
ERROR (payload): column "prefix" does not exist.
Which area(s) are affected? (Select all that apply)
plugin: other, db-postgres
Environment Info
Binaries:
Node: 23.10.0
npm: 10.9.2
Yarn: N/A
pnpm: 10.10.0
Relevant Packages:
payload: 3.38.0
next: 15.3.2
@payloadcms/db-postgres: 3.38.0
@payloadcms/email-nodemailer: 3.38.0
@payloadcms/graphql: 3.38.0
@payloadcms/live-preview: 3.38.0
@payloadcms/live-preview-react: 3.38.0
@payloadcms/next/utilities: 3.38.0
@payloadcms/payload-cloud: 3.38.0
@payloadcms/plugin-cloud-storage: 3.38.0
@payloadcms/plugin-form-builder: 3.38.0
@payloadcms/plugin-nested-docs: 3.38.0
@payloadcms/plugin-redirects: 3.38.0
@payloadcms/plugin-search: 3.38.0
@payloadcms/plugin-seo: 3.38.0
@payloadcms/richtext-lexical: 3.38.0
@payloadcms/storage-s3: 3.38.0
@payloadcms/translations: 3.38.0
@payloadcms/ui/shared: 3.38.0
react: 19.1.0
react-dom: 19.1.0
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000
Available memory (MB): 32768
Available CPU cores: 8