Skip to content

fix(drizzle): resolve blockPath for unique constraint errors#15754

Open
Patrick-Remy wants to merge 2 commits intopayloadcms:mainfrom
Patrick-Remy:add-block-path-to-upsert-error
Open

fix(drizzle): resolve blockPath for unique constraint errors#15754
Patrick-Remy wants to merge 2 commits intopayloadcms:mainfrom
Patrick-Remy:add-block-path-to-upsert-error

Conversation

@Patrick-Remy
Copy link

What?

  • Set full path of ValidationErrors that are thrown on db constraint validation errors.
  • Allow UI to highlight affected block

Why?

Especially when using postgresql with primary keys / unique-constraints at db-level, some duplicate or copy&insert row actions miss to regenerate the copied block ids (e.g. #14574). This currently leads to issues on save/publish when payload tries to insert rows.

Unfortunately the converted and thrown ValidationError doesn't contain a valid path and therefor the backend UI can't highlight the corresponding block and just shows a general message/toast that the value is not unique for field id.

How?

  • Set additional _blockPath metadata to error in upsertRow/index.ts
  • Extract _blockPath and prefix field by it's block path in upsertRow/handleUpsertERror.ts

As a side-effect it looks that there is already some kind of error handling for this scenario. After correcting the error-path, it looks like the frontend/client generates a new ObjectID for the affected block, so that a second publish try is now possible.

Fixes #14574

Highligh block elements when inserting db-rows fails.
Comment on lines +630 to +641
for (const blockRow of blockRows) {
try {
insertedBlockRows[tableName] = await adapter.insert({
db,
tableName,
values: [blockRow.row],
})
} catch (error) {
error._blockPath = blockRow.row._path
throw error
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular insert is the common case where constraint-validation-errors occur. All the other lines are mostly preparation, cleanup, ...

As the upsertRow method is mostly wrapped in two huge try-catch blocks, I decided to append metadata to the originally thrown error and extract it like other metadata in handleUpsertError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: Copy Field in Array Type Causes ID Conflict (PostgreSQL)

1 participant