How to implement a primary key in rails

0

What is better to use in rails for a composite primary key? Use the composite_primary_keys gem? or use an index that contains the columns that I need for the primary key?

What is the best way to uniquely identify a row in a table? , which contains three columns, where the value of each column can be repeated several times, but the combination of Three columns should be a unique combination.

What is the advantage of indexing the three columns of a row in a table?

Many thanks in advance!

    
asked by rrg1459 15.10.2017 в 15:26
source

1 answer

3
  

What is better to use in rails for a composite primary key?

You could use the gem composite_primary_keys , but I do not recommend going against the platform ; I would choose to add a unique composite index (i.e. unique constraint ) that contains the columns you need and leave as the primary key the auto-numeric that generates rails by default.

  

What is the best way to uniquely identify a row in   a table?

With find_by (instead of find ) you can get the single record, specifying the values of each column, for example:

Model.find_by(col_a: value_a, col_b: value_b, col_c: value_c)
  

What is the advantage of indexing the three columns of a row in a table?

The index will speed up the search for records, which will make your application perform better.

    
answered by 15.10.2017 / 18:53
source