From 00c1019e9e2784eee7d0092c00dcf0df2265eff5 Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Mon, 8 Aug 2022 11:59:02 +0200 Subject: [PATCH] fix(mssql): match aggregate errors (#14784) * fix(mssql): match aggregate errors * fix(mssql): wrap for loops in if statements Co-authored-by: Sascha Depold --- src/dialects/mssql/query.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/dialects/mssql/query.js b/src/dialects/mssql/query.js index bea2288dabd3..cf4a8b9032b9 100644 --- a/src/dialects/mssql/query.js +++ b/src/dialects/mssql/query.js @@ -351,6 +351,20 @@ export class MsSqlQuery extends AbstractQuery { }); } + if (err.errors) { + for (const error of err.errors) { + match = error.message.match(/Could not create constraint or index. See previous errors./); + if (match && match.length > 0) { + return new sequelizeErrors.ForeignKeyConstraintError({ + fields: null, + index: match[1], + cause: error, + stack: errStack, + }); + } + } + } + match = err.message.match(/Could not drop constraint. See previous errors./); if (match && match.length > 0) { let constraint = err.sql.match(/(?:constraint|index) \[(.+?)]/i); @@ -367,6 +381,26 @@ export class MsSqlQuery extends AbstractQuery { }); } + if (err.errors) { + for (const error of err.errors) { + match = error.message.match(/Could not drop constraint. See previous errors./); + if (match && match.length > 0) { + let constraint = err.sql.match(/(?:constraint|index) \[(.+?)]/i); + constraint = constraint ? constraint[1] : undefined; + let table = err.sql.match(/table \[(.+?)]/i); + table = table ? table[1] : undefined; + + return new sequelizeErrors.UnknownConstraintError({ + message: match[1], + constraint, + table, + cause: error, + stack: errStack, + }); + } + } + } + return new sequelizeErrors.DatabaseError(err, { stack: errStack }); }