What creates the FOREIGN KEY constraint in Ruby on Rails 3?

前端 未结 1 1518
难免孤独
难免孤独 2020-12-31 21:04

I understand that by default the id field is created and also: PRIMARY KEY (id).

How about the foreign key ?

I have

相关标签:
1条回答
  • 2020-12-31 21:34

    You can use the foreigner gem for adding foreign keys to your application. To get started add the following to your Gemfile

    gem "foreigner"
    

    After that you can easily add foreign keys in your migration like so:

    add_foreign_key :products, :shops
    

    This would add a foreign from product.shop_id to shop.id. See the documentation for more options like differently named keys or self-referencing tables.

    0 讨论(0)
提交回复
热议问题