In Ruby on Rails, how to add foreign key constraint in migration?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- Eager-loading association count with Arel (Rails 3
Would it be enough with adding the following, for example with
Products
andUser
models?add_index :products, :user_id
AFAIK, there isn't any built-in support for that, but there are several plugins that help you with that. You can also add them manually to your migration files, just use the execute method for that, e.g. (sample from Rails API):
This is an update to the matthuhiggins-foreigner gem: http://github.com/sparkfly/foreigner
Features:
Future versions will include CHECK constraints for PostgreSQL, which is needed to implement multi-table inheritance.
Here's a gem-based solution that includes support for adding and removing foreign key constraints, doesn't fail with sqlite, and works correctly with schema.rb files:
http://github.com/matthuhiggins/foreigner