This question already has an answer here:
这个问题在这里已有答案:
- How to set composite key in Rails application 2 answers
- 如何在Rails应用程序2答案中设置组合键
I'm using ActiveRecord in a ruby project without rails. I need to define composite primary key for a table.Normally migration creates primary key automatically. Is it possible to define my own composite primary key for a table using active record?
我在没有rails的ruby项目中使用ActiveRecord。我需要为表定义复合主键。通常,迁移会自动创建主键。是否可以使用活动记录为表定义自己的复合主键?
2 个解决方案
#1
7
There are some gems that give you this ability, such as composite_primary_key.
有一些宝石可以提供这种能力,例如composite_primary_key。
I don't know if Rails 4 has added support for this, would be very interested to hear if it does.
我不知道Rails 4是否增加了对此的支持,如果有的话会非常感兴趣。
#2
1
Question has been asked before, checkout: How to set composite key in Rails application
之前已经问过问题,checkout:如何在Rails应用程序中设置复合键
http://compositekeys.rubyforge.org/ might also be useful.
http://compositekeys.rubyforge.org/也可能有用。
#1
7
There are some gems that give you this ability, such as composite_primary_key.
有一些宝石可以提供这种能力,例如composite_primary_key。
I don't know if Rails 4 has added support for this, would be very interested to hear if it does.
我不知道Rails 4是否增加了对此的支持,如果有的话会非常感兴趣。
#2
1
Question has been asked before, checkout: How to set composite key in Rails application
之前已经问过问题,checkout:如何在Rails应用程序中设置复合键
http://compositekeys.rubyforge.org/ might also be useful.
http://compositekeys.rubyforge.org/也可能有用。