Specify custom primary key in migration

后端 未结 2 885
余生分开走
余生分开走 2020-12-05 16:46

I have a data structure:

  t.integer :userID
  t.string :apikey
  t.integer :characterID

The userID should be the primary key (name is not

相关标签:
2条回答
  • 2020-12-05 17:05

    Instead of circumventing the ActiveRecord model, I made userID a normal column and used

    validates_uniqueness_of :userID, :message => "userID needs to be unique"
    

    on the model to validate it.

    0 讨论(0)
  • 2020-12-05 17:28
     create_table(:my_table, :primary_key => 'userID') do |t|
       # Primary key column will be created automatically
       # Do not create here
       # t.column :userID, :integer, :null => false
       ...
     end
    

    Or

    create_table :my_table, {:id => false} do |t|
      t.integer :userID
      t.string :apikey
      t.integer :characterID
      t.timestamps
     end
     execute "ALTER TABLE my_table ADD PRIMARY KEY (userID);"
    

    And don't forget to put this line somewhere in model:

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