7

I use postgresql 9.3, Ruby 2.0, Rails 4.0.0.

After reading numerous questions on SO regarding setting the Primary key on a table, I generated and added the following migration:

class CreateShareholders < ActiveRecord::Migration
  def change
    create_table :shareholders, { id: false, primary_key: :uid  } do |t|
      t.integer :uid, limit: 8
      t.string :name
      t.integer :shares

      t.timestamps
    end
  end
end

I also added self.primary_key = "uid" to my model.

The migration runs successfully, but when I connect to the DB using pgAdmin III I see that the uid column is not set as primary key. What am I missing?

Alexander Popov
  • 23,073
  • 19
  • 91
  • 130

3 Answers3

19

Take a look at this answer. Try to execute "ALTER TABLE shareholders ADD PRIMARY KEY (uid);" without specifying primary_key parameter in create_table block.

I suggest to write your migration like this (so you could rollback normally):

class CreateShareholders < ActiveRecord::Migration
  def up
    create_table :shareholders, id: false do |t|
      t.integer :uid, limit: 8
      t.string :name
      t.integer :shares

      t.timestamps
    end
    execute "ALTER TABLE shareholders ADD PRIMARY KEY (uid);"
  end

  def down
    drop_table :shareholders
  end
end

UPD: There is natural way (found here), but only with int4 type:

class CreateShareholders < ActiveRecord::Migration
  def change
    create_table :shareholders, id: false do |t|
      t.primary_key :uid
      t.string :name
      t.integer :shares

      t.timestamps
    end    
  end
end
Community
  • 1
  • 1
peresleguine
  • 2,343
  • 2
  • 31
  • 34
  • That's exactly what I missed to write in the question. While I was able to do this, I want to know if there is a way to achieve this in "natural" way, that is without executing direct sql queries. Nevertheless, since there aren't any other suggestions I accept this as a best answer. :) – Alexander Popov Sep 28 '13 at 10:15
  • But what is the type of the primary key in this case? I need it to be bigint. – Alexander Popov Sep 28 '13 at 11:15
  • Sorry, missed it. It is int4. Yet this way is not for us. – peresleguine Sep 28 '13 at 11:25
  • 1
    Since `primary_key` method accepts only one argument, I see no other way but to use plain sql for bigint. – peresleguine Sep 28 '13 at 11:40
  • By default Primary keys in Rails 5.1 are bigint: github.com/rails/rails/pull/26266 – bkdir Sep 22 '17 at 00:13
  • rarrrrr, I can't upvote this enough. We're at Rails 7 and it still no reference in it's docs if you don't want to use `id` as the primary key column on a table – ortonomy Jun 24 '22 at 02:20
1

In my environment(activerecord 3.2.19 and postgres 9.3.1),

:id => true, :primary_key => "columname"

creates a primary key successfully but instead of specifying ":limit => 8" the column' type is int4!

create_table :m_check_pattern, :primary_key => "checkpatternid" do |t|
  t.integer     :checkpatternid, :limit => 8, :null => false
end

Sorry for the incomplete info.

kairya1975
  • 40
  • 7
0

I have created migrations like this:

class CreateShareholders < ActiveRecord::Migration
  def change
    create_table :shareholders, id: false do |t|
      t.integer :uid, primary_key: true
      t.string :name
      t.integer :shares

      t.timestamps
    end    
  end
end
Realms AI
  • 101
  • 4