Define a unique primary key based on 2 columns

后端 未结 7 597
长情又很酷
长情又很酷 2021-01-31 02:52

I would like to define a unique key for records based on 2 columns : \'id\' and \'language\'

to let the user submits the following strings : id=1 language=en value=bl

7条回答
  •  攒了一身酷
    2021-01-31 03:38

    Model

    class User < ActiveRecord::Base
      has_secure_password
      self.primary_keys = :name
    end
    

    Migration

    class CreateUsers < ActiveRecord::Migration
      def change
        create_table :users do |t|
          t.string :name, null: false
          t.string :emailid
          t.string :password_digest
          t.integer :locked, :default => 0
          t.text :secretquestion
          t.string :answer
    
          t.timestamps null: false
        end
        add_index :users, :name, :unique => true
      end
    end
    

    You will get this table

    image

提交回复
热议问题