Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
SchemaPlus provides ActiveRecord support for foreign keys, database defined validations and associations.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.




SchemaPlus is an ActiveRecord extension that provides enhanced capabilities for schema definition and querying, including: enhanced and more DRY index capabilities, support and automation for foreign key constraints, and support for views.

For added rails DRYness see also the gems schema_associations and schema_validations IMPORTANT PRERELEASE NOTE: schema_validations is not yet separate gems, it's currently bundled in here. It's not documented yet though.


SchemaPlus supports all combinations of:

  • rails 3.0 or 3.1

  • MRI ruby 1.8.7 or 1.9.2

  • PostgreSQL, MySQL (using mysql or mysql2 gem), or Sqlite3


Install from via

$ gem install "schema_plus"

or in a Gemfile

gem "schema_plus"


Here some examples that show off the high points. For full details see the RDoc documentation.


With standard rails migrations, you specify indexes separately from the table definition:

# Standard Rails approach...
create_table :parts do |t|
  t.string :name
  t.string :product_code

add_index :parts, :name     # index repeats table and column names and is defined separately
add_index :parts, :product_code, :unique => true

But with SchemaPlus rather than specify your outside your table definition you can specify your indexes when you define each column:

# More DRY way...
create_table :parts do |t|
  t.string :name,           :index => true
  t.string :product_code,   :index => :unique

Options can be provided index using a hash, for example:

t.string :product_code,     :index => { :unique => true, :name => "my_index_name" }

You can also create multi-column indexes, for example:

t.string :first_name
t.string :last_name,        :index => { :with => :first_name }

t.string :country_code
t.string :area_code
t.string :local_number      :index => { :width => [:country_code, :area_code], :unique => true }

If you're using Postgresql, SchemaPlus provides support for conditions, expressions, index methods, and case-insensitive indexes; see doc at SchemaPlus::ActiveRecord::ConnectionAdapters::PostgresqlAdapter and SchemaPlus::ActiveRecord::ConnectionAdapters::IndexDefinition

And when you query column information using ActiveRecord::Base#columns, SchemaPlus analogously provides index information relevant to each column: which indexes reference the column, whether the column must be unique, etc. See doc at SchemaPlus::ActiveRecord::ConnectionAdapters::Column

Foreign Key Constraints

SchemaPlus adds support for foreign key constraints. In fact, for the common convention that you name a column with suffix _id to indicate that it's a foreign key, SchemaPlus automatically defines the appropriate constraint.

You can explicitly specify foreign key constraints, or override the automatic ones, using the :references option to specify the table name (and optionally that table's key column name, if it's not id).

Here are some examples:

t.integer :author_id                                 # automatically references table 'authors', key id
t.integer :parent_id                                 # special name parent_id automatically references its own table (for tree nodes)
t.integer :author, :references => :authors           # non-conventional column name needs :references for a constraint
t.integer :author_id, :refences => :authors          # same as automatic behavior
t.integer :author_id, :refences => [:authors, :id]   # same as automatic behavior
t.integer :author_id, :references => :people         # override table name
t.integer :author_id, :references => [:people, :ssn] # override table name and key
t.integer :author_id, :referencs => nil              # don't create a constraint

You can also modify the behavior using :on_delete, :on_update, and :deferrable

t.integer :author_id, :on_delete => :cascade

The foreign key behavior can be configured globally (see Config) or per-table (see create_table).

To examine your foreign key constraints, connection.foreign_keys returns a list of foreign key constraints defined for a given table, and connection.reverse_foreign_keys returns a list of foreign key constraints that reference a given table. See SchemaPlus::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.


SchemaPlus provides support for creating and dropping views. For example:

create_view :uncommented_posts, "SELECT * FROM posts LEFT OUTER JOIN comments ON comments.post_id = WHERE IS NULL"
drop_view :uncommented_posts

ActiveRecord works with views the same as with ordinary tables. That is, for the above view you can define

class UncommentedPosts < ActiveRecord::Base



SchemaPlus is tested using rspec. To run the tests, after you've forked & cloned: Make sure you have Postgresql and MySQL running. Create database user “schema_plus” with permissions for database “schema_plus_unittest”. Then:

$ cd schema_plus
$ bundle install
$ rake postgresql:build_databases
$ rake mysql:build_databases
$ rake postgresql:spec # to run postgresql tests only
$ rake mysql:spec # to run mysql tests only
$ rake mysql2:spec # to run mysql2 tests only
$ rake sqlite3:spec # to run sqlite3 tests only
$ rake spec # run all tests

If you're running ruby 1.9.2, code coverage results will be in coverage/index.html – it should be at 100% coverage.


This plugin is released under the MIT license.

Something went wrong with that request. Please try again.