# ActiveRecord Constraints

**URL:** <https://www.ruby-forum.com/t/activerecord-constraints/166432>\
**Category:** Rails\
**Created:** [April 25, 2009, 5:16am UTC](https://www.ruby-forum.com/t/activerecord-constraints/166432 "2009-04-25T05:16:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [April 25, 2009, 5:16am UTC](https://www.ruby-forum.com/t/activerecord-constraints/166432/1 "2009-04-25T05:16:00Z")

</div>

A plugin to easily add constraints into a PostgreSQL database. I think  
it would be easy to add other database engines to it assuming they  
implement constraints.

The GIT repository is here:

> **[GitHub - pedz/activerecord\_constraints: Create database constraints in Rails...](https://github.com/pedz/activerecord_constraints)**
>
> Create database constraints in Rails migrations and handle the exceptions they produce - GitHub - pedz/activerecord\_constraints: Create database constraints in Rails migrations and handle the excep...

The rdoc is here:

[http://pedz.github.com/activerecord\_constraints](http://pedz.github.com/activerecord_constraints)

A small snippet from the readme:

Unique Constraint:

```
class CreateFoos < ActiveRecord::Migration
  def self.up
    create_table :foos do |t|
      # name will have a "unique" constraint
      t.string :name, :null => false, :unique => true
    end
  end
end

```

Trivial foreign key constraint:

```
class CreateFoos < ActiveRecord::Migration
  def self.up
    create_table :foos do |t|
      # bar_id will now be a foreign key constraint column id in

```

table bar  
t.integer :bar\_id, :null =\> false, :reference =\> true  
end  
end  
end
