SQL constraints through migrations

21 views
Skip to first unread message

Sumit Srivastava

unread,
Feb 25, 2013, 5:17:28 AM2/25/13
to rubyonra...@googlegroups.com
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

Walter Lee Davis

unread,
Feb 25, 2013, 8:33:31 AM2/25/13
to rubyonra...@googlegroups.com
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

>
> Regards,
> Sumit
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/uRY6KUVQ5QIJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Valera Prokopchuk

unread,
Jan 23, 2015, 3:33:37 PM1/23/15
to rubyonra...@googlegroups.com
There is special gem that makes it possible. See https://github.com/vprokopchuk256/mv-core for details. 

With that gem you can do it in this way: 

def up
  change_table :table_name do
     t.change :check_constraint_name, :integer, inclusion: { in: [1, 2, 3], as: :check } 
  end
end

Best regards, 
Valeriy Prokopchuk

понеділок, 25 лютого 2013 р. 12:17:28 UTC+2 користувач Sumit Srivastava написав:

Andrey Nering

unread,
Jan 24, 2015, 11:21:48 AM1/24/15
to rubyonra...@googlegroups.com
I didn't know that gem, I'll take a look on that.

With Postgres, you can use this one too: https://github.com/take-five/postgresql-check
Reply all
Reply to author
Forward
0 new messages