Module Sequel::Plugins::PgAutoConstraintValidations
In: lib/sequel/plugins/pg_auto_constraint_validations.rb

The pg_auto_constraint_validations plugin automatically converts some constraint violation exceptions that are raised by INSERT/UPDATE queries into validation failures. This can allow for using the same error handling code for both regular validation errors (checked before attempting the INSERT/UPDATE), and constraint violations (raised during the INSERT/UPDATE).

This handles the following constraint violations:

  • NOT NULL
  • CHECK
  • UNIQUE (except expression/functional indexes)
  • FOREIGN KEY (both referencing and referenced by)

If the plugin cannot convert the constraint violation error to a validation error, it just reraises the initial exception, so this should not cause problems if the plugin doesn‘t know how to convert the exception.

This plugin is not intended as a replacement for other validations, it is intended as a last resort. The purpose of validations is to provide nice error messages for the user, and the error messages generated by this plugin are fairly generic. The error messages can be customized using the :messages plugin option, but there is only a single message used per constraint type.

This plugin only works on the postgres adapter when using the pg 0.16+ driver, PostgreSQL 9.3+ server, and PostgreSQL 9.3+ client library (libpq). In other cases it will be a no-op.

Example:

  album = Album.new(:artist_id=>1) # Assume no such artist exists
  begin
    album.save
  rescue Sequel::ValidationFailed
    album.errors.on(:artist_id) # ['is invalid']
  end

Usage:

  # Make all model subclasses automatically convert constraint violations
  # to validation failures (called before loading subclasses)
  Sequel::Model.plugin :pg_auto_constraint_validations

  # Make the Album class automatically convert constraint violations
  # to validation failures
  Album.plugin :pg_auto_constraint_validations

Methods

configure  

Classes and Modules

Module Sequel::Plugins::PgAutoConstraintValidations::ClassMethods
Module Sequel::Plugins::PgAutoConstraintValidations::InstanceMethods

Constants

DEFAULT_ERROR_MESSAGES = { :not_null=>"is not present", :check=>"is invalid", :unique=>'is already taken', :foreign_key=>'is invalid', :referenced_by=>'cannot be changed currently'   The default error messages for each constraint violation type.

Public Class methods

Setup the constraint violation metadata. Options:

:messages :Override the default error messages for each constraint violation type (:not_null, :check, :unique, :foreign_key, :referenced_by)

[Source]

    # File lib/sequel/plugins/pg_auto_constraint_validations.rb, line 64
64:       def self.configure(model, opts=OPTS)
65:         model.instance_exec do
66:           setup_pg_auto_constraint_validations
67:           @pg_auto_constraint_validations_messages = (@pg_auto_constraint_validations_messages || DEFAULT_ERROR_MESSAGES).merge(opts[:messages] || OPTS).freeze
68:         end
69:       end

[Validate]