feat: add constraint violation database exceptions#10359
Open
memleakd wants to merge 6 commits into
Open
Conversation
- Add a ConstraintViolationException hierarchy for typed integrity errors. - Classify foreign key, not-null, check, unique, and generic constraint violations per driver. - Keep getLastException() aligned with thrown exceptions when DBDebug is disabled. - Update database docs, examples, changelog, and focused tests. Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
michalsn
reviewed
Jun 30, 2026
michalsn
left a comment
Member
There was a problem hiding this comment.
Overall, I like this direction. Could we add a few more live tests where the existing table structure allows it, particularly for NOT NULL updates and foreign-key violations?
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Contributor
Author
|
Thanks for the review @michalsn. I pushed updates for the cases you pointed out. While checking the surrounding driver codes, I also added MySQL One small detail: I guarded MariaDB References I checked:
|
patel-vansh
approved these changes
Jul 1, 2026
michalsn
reviewed
Jul 1, 2026
fdac7fa to
4986295
Compare
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
4986295 to
ffb4b0d
Compare
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.
Description
This builds on #9979 by extending the same typed-exception approach to other common database constraint violations.
Database constraint failures can now be represented by more specific exception types when the driver provides enough information:
UniqueConstraintViolationExceptionForeignKeyConstraintViolationExceptionNotNullConstraintViolationExceptionCheckConstraintViolationExceptionAll of them extend the new
ConstraintViolationException, which still extendsDatabaseException.UniqueConstraintViolationExceptionkeeps the same public purpose and remains catchable as aDatabaseException.This lets applications handle common integrity errors directly with
catchblocks instead of inspecting raw driver-specific error codes or matching error messages.When a driver reports an integrity constraint violation that CodeIgniter cannot safely classify more precisely, the parent
ConstraintViolationExceptionis used instead. This keeps the behavior conservative.The same typed exception is also available through
getLastException()whenDBDebugis disabled.I kept the classification close to the existing
UniqueConstraintViolationExceptionimplementation, but I am happy to adjust the hierarchy or naming if maintainers prefer a different shape.Checklist: