Fixed Incorrect DuplicateKeyException type when inserting null into a non-nullable field

Affected version


Well-known member
Consider the (contrived) sequence;

$db = \XF::db();

$db->query(' CREATE TEMPORARY TABLE nullable_test (
    col int not null

$db->query('insert into nullable_test (col) values (null)');
The resulting exception is of the wrong type;
An exception occurred: [XF\Db\DuplicateKeyException] MySQL query error [1048]: Column 'col' cannot be null in src/XF/Db/AbstractStatement.php on line 217
#0 src/XF/Db/Mysqli/Statement.php(196): XF\Db\AbstractStatement->getException('MySQL query err...', 1048, '23000')
#1 src/XF/Db/Mysqli/Statement.php(77): XF\Db\Mysqli\Statement->getException('MySQL query err...', 1048, '23000')
#2 src/XF/Db/AbstractAdapter.php(94): XF\Db\Mysqli\Statement->execute()
#3 test.php(17): XF\Db\AbstractAdapter->query('insert into nul...')
#4 {main}
Using MariaDB 10.4.x, and reported to occur with 10.3.x

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue. It has now been resolved and we are aiming to include it in a future XF release (2.1.5).

Change log:
Prevent false positive detection of duplicate key exceptions in queries due to generic SQL state code.
Any changes made as a result of this issue being resolved may not be rolled out here until later.