From 00a5fd3d18ac908af688c5944922cf69c56e850b Mon Sep 17 00:00:00 2001 From: Michael Schuerig Date: Sun, 5 Apr 2009 01:42:21 +0200 Subject: Translate foreign key violations to ActiveRecord::InvalidForeignKey exceptions. Signed-off-by: Michael Koziarski --- activerecord/lib/active_record/base.rb | 4 ++++ activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 2 ++ .../lib/active_record/connection_adapters/postgresql_adapter.rb | 2 ++ 3 files changed, 8 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index defe1b56b6..027ab8af9e 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -71,6 +71,10 @@ module ActiveRecord #:nodoc: class RecordNotUnique < StatementInvalid end + # Raised when a record cannot be inserted or updated because it references a non-existent record. + class InvalidForeignKey < StatementInvalid + end + # Raised when number of bind variables in statement given to :condition key (for example, when using +find+ method) # does not match number of expected variables. # diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 59bf01e774..ae065e6bc1 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -569,6 +569,8 @@ module ActiveRecord case exception.errno when 1062 RecordNotUnique.new(message) + when 1452 + InvalidForeignKey.new(message) else super end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e990052e09..24482aa19a 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -945,6 +945,8 @@ module ActiveRecord case exception.message when /duplicate key value violates unique constraint/ RecordNotUnique.new(message) + when /violates foreign key constraint/ + InvalidForeignKey.new(message) else super end -- cgit v1.2.3