Forum: Ruby on Rails SQL constraints through migrations

Posted by Sumit Srivastava (Guest)
on 2013-02-25 11:18
(Received via mailing list)
Hi,

I have to add contraints (CHECK) on a sql table through rails migration.
How should I? I googled and got to know about using something as 
follows,

def self.up
    execute "ALTER TABLE table_name ADD CONSTRAINT check_constraint_name
CHECK (check_column_name IN (1, 2, 3) )"
end

But am not sure if it appropriate to implement it this way or if there 
is
any better method to get it done?

Regards,
Sumit
Posted by Walter Davis (walterdavis)
on 2013-02-25 14:35
(Received via mailing list)
On Feb 25, 2013, at 5:17 AM, Sumit Srivastava wrote:

> Hi,
>
> I have to add contraints (CHECK) on a sql table through rails migration. How 
should I? I googled and got to know about using something as follows,
>
> def self.up
>     execute "ALTER TABLE table_name ADD CONSTRAINT check_constraint_name CHECK 
(check_column_name IN (1, 2, 3) )"
> end
>
> But am not sure if it appropriate to implement it this way or if there is any 
better method to get it done?

I haven't heard it mentioned in a while, but the foreigner gem was 
popular here last year. You might want to look at that.

Walter
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.