All posts

Creating a table with non id primary key in Rails

In order two change a database tables primary key from id to another desired attribute when on create you must first pass an option of id: false as a second argument to the create_table method. Then when declaring columns for your table, indicate which should become the primary key by passing it an option of primary_key: true.

  def change
    create_table :applications, id: false do |t|
      t.string  :uid,     null: false, primary_key: true

Then within your applications ActiveRecord model you will need to add a delcaration at the top of the model indicating the new primary key. This helps keep things clear for others working on your application.

class Application
  self.primary_key = "uid"

Similar posts:


I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).