[RAILS] primary key memo

Again, make a note to make sure you understand So that it can be verbalized

What is a primary key

DB key Identify the ** data (rows, records) ** of the DB ** uniquely (no other data is the same) ** Items for

[For some reason] Speaking of "data whose primary key value is XX" Because you can immediately judge "Oh, this data (row, record)"

Also called "** primary key **"


・ Table ** Search speed ** is significantly ** faster ** -Guarantee ** integrity ** of table data

Rails ・ Specifications that id is added by default -You may want to set a character string or something as a ** primary key . → You can ** cancel the setting to automatically set the primary key as an ID with " id: false " ** → Set " primary_key: true **" for the column you want to set as the primary key.

migration file

class Hoge < ActiveRecord::Migration[5.2]
  def change
    # id:Add false
    create_table :hogehoge, id: false do |t| 
      # primary_key:Add true
      t.integer :user_id, null: false, primary_key: true
      t.string :name

Note ・ ** Composite keys (composite_primary_keys) ** ・ ** Gem ** called composite_primary_keys

Reference article

https://wa3.i-3-i.info/word1991.html https://www.sejuku.net/blog/52356 http://www.code-magagine.com/?p=12207 https://qiita.com/tanutanu/items/6c53b5290eaa9bf905d3 https://qiita.com/belion_freee/items/8f8f1d1e5333da561fd8

Recommended Posts

primary key memo
Integer memo
docker memo
Lombok memo
Dockerfile memo
Iterator memo
corretto memo
Java memo
AWS memo
Dcokerfile memo
Ruby memo
Memo Stream
How to generate a primary key using @GeneratedValue