Skip to content

Commit 63b76aa

Browse files
RasmusNRasmus Nordström
andauthored
Add error code 2601 (#153)
SQL Server have two slightly different error codes for duplicate key errors: 2627 occurs on unique/PK constraint violations, while 2601 is triggered by unique index violations. Both indicate duplicate keys but stem from different schema definitions. Co-authored-by: Rasmus Nordström <[email protected]>
1 parent 4937266 commit 63b76aa

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

error_translator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// The error codes to map mssql errors to gorm errors, here is a reference about error codes for mssql https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors?view=sql-server-ver16
1010
var errCodes = map[int32]error{
1111
2627: gorm.ErrDuplicatedKey,
12+
2601: gorm.ErrDuplicatedKey,
1213
547: gorm.ErrForeignKeyViolated,
1314
}
1415

error_translator_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func TestDialector_Translate(t *testing.T) {
2727
args: args{err: mssql.Error{Number: 2627}},
2828
want: gorm.ErrDuplicatedKey,
2929
},
30+
{
31+
name: "it should return ErrDuplicatedKey error if the error number is 2601",
32+
args: args{err: mssql.Error{Number: 2601}},
33+
want: gorm.ErrDuplicatedKey,
34+
},
3035
{
3136
name: "it should return ErrForeignKeyViolated the error number is 547",
3237
args: args{err: mssql.Error{Number: 547}},

0 commit comments

Comments
 (0)