fix(drizzle): resolve blockPath for unique constraint errors#15754
Open
Patrick-Remy wants to merge 2 commits intopayloadcms:mainfrom
Open
fix(drizzle): resolve blockPath for unique constraint errors#15754Patrick-Remy wants to merge 2 commits intopayloadcms:mainfrom
Patrick-Remy wants to merge 2 commits intopayloadcms:mainfrom
Conversation
Highligh block elements when inserting db-rows fails.
Patrick-Remy
commented
Feb 24, 2026
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 | ||
| } | ||
| } |
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
pathofValidationErrors that are thrown on db constraint validation errors.Why?
Especially when using
postgresqlwith primary keys / unique-constraints at db-level, some duplicate or copy&insert row actions miss to regenerate the copied blockids (e.g. #14574). This currently leads to issues on save/publish when payload tries to insert rows.Unfortunately the converted and thrown
ValidationErrordoesn't contain a validpathand therefor the backend UI can't highlight the corresponding block and just shows a general message/toast that the value is not unique for fieldid.How?
_blockPathmetadata to error inupsertRow/index.ts_blockPathand prefix field by it's block path inupsertRow/handleUpsertERror.tsAs 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