diff options
author | Michael Schuerig <michael@schuerig.de> | 2009-04-05 01:42:21 +0200 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2009-06-26 16:52:55 +1200 |
commit | 00a5fd3d18ac908af688c5944922cf69c56e850b (patch) | |
tree | ab0acf2af0a1404701fe04386e10e1f12084b630 /activerecord/lib/active_record | |
parent | 53a3eaa8603cf6e7a3c007f327fe5d3bb68de1ee (diff) | |
download | rails-00a5fd3d18ac908af688c5944922cf69c56e850b.tar.gz rails-00a5fd3d18ac908af688c5944922cf69c56e850b.tar.bz2 rails-00a5fd3d18ac908af688c5944922cf69c56e850b.zip |
Translate foreign key violations to ActiveRecord::InvalidForeignKey exceptions.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 8 insertions, 0 deletions
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 <tt>:condition</tt> 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 |