Rails4 create connection tab no need to add primary key to Migration?

I am using this command:

rails g migration CreateJoinTableUserPloy user ploy 

And I check the migration file:

  create_join_table :Users, :Posts do |t| #t.index [:user_id, :ploy_id] #t.index [:ploy_id, :user_id] end 

There are 2 indexes that are commented on defualt.

Then I ran the following command:

 rake db:migrate 

Then I check the structure of my database

And I did not see the primary key. Does this mean that in the join column there is no need to add the index and primary key to the database structure?

+2
source share
1 answer

According to http://meta.serverfault.com/a/1931 , I will answer this as an answer, although some of the information is in the comment thread.

The connection table created by Rails does not have a primary key (i.e. created by create_join_table , which is used to migrate with JoinTable in their name), and Rails does not have an inherent requirement for one. This is because for most pure join tables only the join table identifier is used, in which case primary_key is not required. You can, of course, add and label the primary key column if you wish.

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

In addition, there is no fundamental need to create an index. You can create it, if you want, and speed up access to records due to the additional time spent on creating and updating a record. See fooobar.com/questions/52275 / ... for more details.

+3
source

Source: https://habr.com/ru/post/949373/


All Articles