# ActiveRecord suggestion: column options

**URL:** <https://www.ruby-forum.com/t/activerecord-suggestion-column-options/89928>\
**Category:** Rails\
**Created:** [March 23, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/activerecord-suggestion-column-options/89928 "2007-03-23T21:26:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [March 23, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/activerecord-suggestion-column-options/89928/1 "2007-03-23T21:26:15Z")

</div>

Hello,

I’m using rails-1.2.2 with ActiveRecord-1.15.2. I have a small feature  
request for ActiveRecord.

When creating a table, AR lets you add options to the whole table, but  
not for individual columns. I would like to be able to write:

t. column foo\_id, :integer,  
:options =\> “CONSTRAINT fk\_foo\_id REFERENCES a(id) ON DELETE  
CASCADE”

Why? Because this syntax is accepted by both Oracle and Sqlite3  
(although Sqlite3 silently ignores it), so the migration is portable.

However, if create the table first, and then do

```
execute %{alter table #{from_table}
          add constraint #{constraint_name}
          foreign key (#{from_column}) references #{to_table}

```

(#{to\_column})}

Sqlite3 rejects this with an exception. Sure I can rescue this:

rescue ActiveRecord::StatementInvalid =\> e  
raise unless /SQLite/ =~ e

It’s just a bit ugly. But this is a minor point. AR rocks 🙂

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [March 24, 2007, 1:23am UTC](https://www.ruby-forum.com/t/activerecord-suggestion-column-options/89928/2 "2007-03-24T01:23:06Z")

</div>

Sounds interesting but is perhaps a fringe requirement. It’s quite  
common to enforce constraints in the application in rails rather than  
the database. It also seems at first look to have limited database  
support.

However, I’m sure it would be interesting to see a plugin that extends  
the core migrations if you’re up for it.

Steve

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [March 24, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/activerecord-suggestion-column-options/89928/3 "2007-03-24T16:22:48Z")

</div>

Brian,

There is a foreign\_key\_migrations plugin for that and it works great for  
me. I am using it on postgres.

[http://www.redhillonrails.org/](http://www.redhillonrails.org/)

-Wes

James “Wes” Hays  
Great Basin Development  
P.O.Box 3503, Reno, Nevada 89505  
Cell: (775) 745-3013  
removed\_email\_address@domain.invalid  
[http://www.gbdev.com](http://www.gbdev.com)

----- Original Message -----  
From: “candlerb” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “Ruby on Rails: Talk” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Friday, March 23, 2007 1:24:53 PM (GMT-0800) America/Los\_Angeles  
Subject: [Rails] ActiveRecord suggestion: column options

Hello,

I’m using rails-1.2.2 with ActiveRecord-1.15.2. I have a small feature  
request for ActiveRecord.

When creating a table, AR lets you add options to the whole table, but  
not for individual columns. I would like to be able to write:

t. column foo\_id, :integer,  
:options =\> “CONSTRAINT fk\_foo\_id REFERENCES a(id) ON DELETE  
CASCADE”

Why? Because this syntax is accepted by both Oracle and Sqlite3  
(although Sqlite3 silently ignores it), so the migration is portable.

However, if create the table first, and then do

```
execute %{alter table #{from_table}
          add constraint #{constraint_name}
          foreign key (#{from_column}) references #{to_table}

```

(#{to\_column})}

Sqlite3 rejects this with an exception. Sure I can rescue this:

rescue ActiveRecord::StatementInvalid =\> e  
raise unless /SQLite/ =~ e

It’s just a bit ugly. But this is a minor point. AR rocks 🙂

Regards,

Brian.
