Rails4 create join tabel no need to add primary key in Migration?

后端 未结 1 1285
北荒
北荒 2021-01-21 09:22

I use this command :

 rails g migration CreateJoinTableUserPloy user ploy

And i check the Migration file:

    create_join_table         


        
相关标签:
1条回答
  • 2021-01-21 10:09

    Consistent with http://meta.serverfault.com/a/1931, I'm going to respond to this as an answer, even though some of the information is in the comment thread.

    There is no primary key in a Rails-generated join table (i.e. as created by create_join_table, which is used by migrations with JoinTable in their name) and Rails has no inherent requirement for one. That's because most pure join tables are only accessed by the id's of the joined tables, in which case primary_key is unnecessary. You can, of course, add and designate a primary key column if you wish.

    Rails does not support multiple column primary_keys, although there are multiple gems that provide that support, such as https://github.com/composite-primary-keys/composite_primary_keys.

    Further, there is no fundamental need to create an index. You can create one if you wish, and it will speed up access to records at the cost of some additional time spent on record creation and update. See https://stackoverflow.com/a/3658980/1008891 for more discussion of this.

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