Seeder: required fields with references are created complete (1.15.5)
A page whose required layout holds a { $page } / { $form } reference could not be
created: pass 1 left the whole field out, validation rejected the record, and the
dry run reported a create (vanstevpl-web, 1.15.4).
- pass 1 resolves references to records that exist (or were created earlier in
the run), so the field is written complete
- a { $page } to a page later in the same call is null in pass 1 and filled in
pass 2; a record whose required field needs it is retried once that page exists
- required links in a cycle and references to pages or forms that never appear
are an error with nothing written; unknown page targets are found while
planning, so the dry run reports them too
- dry run: a missing form is an error (was a placeholder); a form planned earlier
in the same dry run counts as existing
- real-Payload tests (pl, en, de): the report's careers page, an existing form
target, optional and required forward links, a cycle, missing page and form,
create-only second runs, dry-run parity
- CHANGELOG Unreleased, seeding.md, api.md; reference skills refreshed
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
179d5b0d79
commit
748ef9286f
@@ -8,6 +8,17 @@ versions at once, go through every "Requires project changes" section in order,
|
||||
version you have. A new release = a new section at the top; we remove the oldest ones only
|
||||
once there are more than 20.
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Fixed
|
||||
- **Seeding a required field with references** (`createSeeder`, `upsertBySlug` / `upsertBy`): a page whose required
|
||||
`layout` holds a `{ $page }` or `{ $form }` reference can be created. The first pass used to leave the whole field out,
|
||||
so validation rejected the record ("The following field is invalid: … > Layout") while the dry run reported a create.
|
||||
Now references to records that exist are resolved in the first pass, so the field is written complete. A link to a
|
||||
page created later in the same call is filled in the second pass, and a record whose required field needs that page
|
||||
waits until it exists. Required links in a cycle, and links to a page or form that never appears, are an error with
|
||||
nothing written. The dry run now reports the same outcome, including a missing form. Seed scripts need no change.
|
||||
|
||||
## 1.15.4
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -558,7 +558,7 @@ Server only (`pnpm payload run`); never import it from client code, the Payload
|
||||
| Export | Import from | What it does / when |
|
||||
|---|---|---|
|
||||
| `createSeeder(payload, { dryRun?, update?, force?, only?, guard?, editCheck?, context?, pagesCollection?, formsCollection?, label?, log? })` | `@intecion/ipal-kit/seed` | The seed rules in one place → `{ upsertBySlug, upsertBy, fillGlobal, summary, dryRun, update, force, local, only }`. `only` (or `SEED_ONLY`) = the records the run may touch (`pages:kontakt`, `forms:<title>`, `global:<slug>`, `pages:*`), the rest → `skip (not listed)`; a run without it against production / a non-local database throws unless `IPAL_SEED_ALLOW_FULL=1` / `guard.allowFull: true` (after launch: named records only; dry runs are never refused; `guard: false` turns every guard off). Create-only by default, natural keys, never deletes, globals: empty fields only, every locale with `fallbackLocale: false` onto the same row ids, skips records edited since seeding, writes with `context` `{ ipalSeed, disableRevalidate, ipalSkipIndexNow }`. Throws when `update` runs on production / a non-local database without `IPAL_SEED_ALLOW_PROD_UPDATE=1`. Both seed templates use it. |
|
||||
| `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). |
|
||||
| `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). A required field (layout) with references is written complete in the first write; a link to a page later in the same call waits for it; required cycles and unknown targets are an error with nothing written; the dry run reports the same. |
|
||||
| `upsertBy(collection, key, data)` | (method) | Matches by other keys, e.g. a form by `{ title }`. |
|
||||
| `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does; a field already holding the seeded value is skipped (all seeded fields with `update` when untouched). |
|
||||
| `summary()` | (method) | Prints and returns `{ created, skipped, updated, skippedEdited, errors }`; exit 1 when `errors`. |
|
||||
|
||||
+14
-2
@@ -203,12 +203,24 @@ language the same rows in the same order (its texts differ; non-localized values
|
||||
from the last language written). A field with `localized: true` keeps its own rows per language.
|
||||
`{ $page: 'kontakt' }` becomes the ID of the page whose default-locale slug is `kontakt`;
|
||||
`{ $form: 'Contact form' }` becomes the ID of the form with that title (seed the form first). A missing form
|
||||
is an error for that record, never an empty relation. Ready pages made of library blocks:
|
||||
is an error for that record, never an empty relation.
|
||||
|
||||
**References and required fields.** A new record is created complete, so a required field (a `layout` whose block
|
||||
links to a page or form) passes validation:
|
||||
- a reference to a record that exists — or was created earlier in the run — becomes its ID in the first write;
|
||||
- a `{ $page }` to a page created later **in the same `upsertBySlug` call** is filled in the second pass; when the
|
||||
field that holds it is required, the record waits until that page exists. Pages that link to each other belong in
|
||||
one call (an array);
|
||||
- required links in a cycle (A needs B, B needs A) and a reference to a page or form that does not exist and is not
|
||||
planned are an **error** for those records, and nothing is written for them. Make one of the links optional, or seed
|
||||
one of the pages first;
|
||||
- the dry run reports the same outcome. A form planned earlier in the same dry run counts as existing. Only a required
|
||||
cycle shows up first in the real run, because the dry run does not validate fields. Ready pages made of library blocks:
|
||||
`pageTemplates` in [block-library.md](./block-library.md#page-templates).
|
||||
|
||||
| Method | What it does |
|
||||
|---|---|
|
||||
| `upsertBySlug(collection, data \| data[], { slugField })` | matches by the default-locale slug (`slugField`, default `slug`). With an array, missing records are created first (without fields that hold `$page` / `$form` links), then every record gets its full content, so pages can link to each other in any order |
|
||||
| `upsertBySlug(collection, data \| data[], { slugField })` | matches by the default-locale slug (`slugField`, default `slug`). With an array, missing records are created first (references to existing records resolved, links to pages later in the call filled in pass 2; a required one waits for its page), then every record gets its full content, so pages can link to each other in any order. Unknown targets and required cycles: error, nothing written |
|
||||
| `upsertBy(collection, key, data)` | matches by other natural keys, e.g. `{ title: 'Contact form' }` (all keys must match, default locale) |
|
||||
| `fillGlobal(slug, data)` | fills only the global's empty fields (the default locale decides). A choice — select, radio, checkbox, number, date — still at the field's `defaultValue` counts as empty (Payload stores defaults on the first save, so `defaultTheme: 'system'` can be seeded over `light`); text is never treated so, even when it equals a default. A field that already holds exactly the seeded value is not written (`skip (exists)`), so re-running a seed of defaults changes nothing. With `update` and an untouched global: every seeded field |
|
||||
| `summary()` | prints the summary and returns `{ created, skipped, updated, skippedEdited, errors }` |
|
||||
|
||||
Reference in New Issue
Block a user