Skip to content

Commit 0c35cc2

Browse files
authored
Merge pull request #787 from aidanharan/foreign-key-violation-delete
Rails 6: Raise exception for foreign key constraint violation
2 parents 7026eb5 + 9061a7a commit 0c35cc2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,15 @@ def initialize_type_map(m = type_map)
332332
def translate_exception(e, message:, sql:, binds:)
333333
case message
334334
when /(cannot insert duplicate key .* with unique index) | (violation of unique key constraint)/i
335-
RecordNotUnique.new(message)
336-
when /conflicted with the foreign key constraint/i
337-
InvalidForeignKey.new(message)
335+
RecordNotUnique.new(message, sql: sql, binds: binds)
336+
when /(conflicted with the foreign key constraint) | (The DELETE statement conflicted with the REFERENCE constraint)/i
337+
InvalidForeignKey.new(message, sql: sql, binds: binds)
338338
when /has been chosen as the deadlock victim/i
339-
DeadlockVictim.new(message)
339+
DeadlockVictim.new(message, sql: sql, binds: binds)
340340
when /database .* does not exist/i
341-
NoDatabaseError.new(message)
341+
NoDatabaseError.new(message, sql: sql, binds: binds)
342342
when /data would be truncated/
343-
ValueTooLong.new(message)
343+
ValueTooLong.new(message, sql: sql, binds: binds)
344344
when /Column '(.*)' is not the same data type as referencing column '(.*)' in foreign key/
345345
pk_id, fk_id = SQLServer::Utils.extract_identifiers($1), SQLServer::Utils.extract_identifiers($2)
346346
MismatchedForeignKey.new(
@@ -352,9 +352,9 @@ def translate_exception(e, message:, sql:, binds:)
352352
primary_key: pk_id.object
353353
)
354354
when /Cannot insert the value NULL into column.*does not allow nulls/
355-
NotNullViolation.new(message)
355+
NotNullViolation.new(message, sql: sql, binds: binds)
356356
when /Arithmetic overflow error/
357-
RangeError.new(message)
357+
RangeError.new(message, sql: sql, binds: binds)
358358
else
359359
super
360360
end

0 commit comments

Comments
 (0)