Skip to content

Commit 658a40b

Browse files
committed
fix: classify SQLSRV foreign key violations
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 77c1ddc commit 658a40b

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

system/Database/SQLSRV/Connection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ protected function isUniqueConstraintViolation(int|string $code, string $message
117117
return false;
118118
}
119119

120+
/**
121+
* Checks whether the native database error represents a foreign key constraint violation.
122+
*/
123+
protected function isForeignKeyConstraintViolation(int|string $code, string $message): bool
124+
{
125+
return $this->getVendorErrorCode($code) === 547
126+
&& $this->hasSQLState($code, '23000')
127+
&& str_contains($message, 'FOREIGN KEY constraint');
128+
}
129+
120130
/**
121131
* Checks whether the native database error represents a NOT NULL constraint violation.
122132
*/

tests/system/Database/ConstraintViolationExceptionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ public static function provideCreatesConstraintViolationExceptions(): iterable
225225
UniqueConstraintViolationException::class,
226226
];
227227

228+
yield 'SQLSRV foreign key' => [
229+
self::connection(SQLSRVConnection::class, 'SQLSRV'),
230+
'23000/547',
231+
'The INSERT statement conflicted with the FOREIGN KEY constraint.',
232+
ForeignKeyConstraintViolationException::class,
233+
];
234+
228235
yield 'SQLSRV generic constraint' => [
229236
self::connection(SQLSRVConnection::class, 'SQLSRV'),
230237
'23000/547',

0 commit comments

Comments
 (0)