Ruby on Rails
DatabaseConstraintReflection

In databases that allow such a feature, it would be keen if ActiveRecord reflected over the constraints of columns/attributes.

This way, the various validations and associations present in your table structure’s DDL SQL need not be repeated in your model classes. Of course this isn’t possible on all databases, but should be possible on most of the popular ones (ie. PostgreSQL and MySQL with InnoDB tables).

—-

I second this idea. I’ve been thinking about this a bit recently and it does seem a bit of pain to have to repeat yourself somewhat. The sticking point for me at the moment is the question of how you’d specify error messages for the constraints inherited from the database. Seems like you’d end up writing pretty much the same code as a validation currently contains anyway. Goynang

—-

On the other hand, there are quite a few constraints and restrictions imposed by table structure (i.e. the data model itself) that can easily be reflected in ActiveRecord.

One such thing is column sizes:


module ActiveRecord
  module Validations
    module ClassMethods

      def validates_sizes
        columns.each do |column|
          case column.type
            when :text, :string, :binary
              validates_length_of column.name, :maximum => column.limit unless column.limit.nil?
          end
        end
      end

    end
  end
end

One can also easily imagine: