How to set a primary key in ruby ​​for rail migration?

How to set the primary key for my IdClient field? I tried all the methods, but I will get errors (rails 3.0.9) ... Could you help me?

class CreateCustomers < ActiveRecord::Migration
  def self.up
    create_table :customers do |t|
      t.integer :IdCustomer
      t.string :username
      t.string :crypted_password
      t.string :password_salt
      t.string :persistence_token
      t.string :email
      t.string :Skype
      t.string :ICQ
      t.string :Firstname
      t.string :Lastname
      t.string :Country
      t.string :State
      t.string :City
      t.string :Street
      t.string :Building
      t.integer :Room
      t.string :AddressNote
      t.date :DateOfReg
      t.integer :CustGroup
      t.float :TotalBuy

      t.timestamps
      add_index(:customers, :IdCustomer, :unique => true)
    end
  end

  def self.down
    drop_table :customers
  end
end

Also how to establish relationships in the model?

+5
source share
1 answer

Do not do that. Use the built-in field as the primary key id. If you intend to use Rails, you should create a "Rails way" application if you have no reason to.

If you really want to do this, you can pass the :primary_keyoption create_table:

create_table :customers, :primary_key => :idClient do |t|
  # ...
end

You also need to give your model the name of your primary key with self.primary_key = "idClient"

+16
source

All Articles