Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:46:02+00:00 2026-06-08T07:46:02+00:00

This is a build on a question I asked before (without any luck). I

  • 0

This is a build on a question I asked before (without any luck).

I have a model where I’m using a string as a primary key:

class Employee < ActiveRecord::Base
   self.primary_key = "employment_id"
end 

This table also contains the Rails default ‘id’ field, with a uniqueness constraint.

When I add a new employee locally it all works fine, with Rails automatically generating a new unique id.

However when I run this on Heroku Postgres it appears to treat ‘id’ and ’employment_id’ as the same field. I tried to get around this by manually setting a unique id, but still get this behaviour:

Employee.new do |s|

  max_id = Employee.maximum(:id)  
  puts max_id.to_s             # => 1803 

  s.employment_id = "fred_01"      
  s.id = max_id + 1 

  puts employment_id.to_s      # => 1804

end

I’m running postgres 9.1.3 locally (and Heroku is on 9.1.4). I’m on Rails 3.2.3.

My questions are:

  • Any idea what’s going on here?
  • Do you think it’s right that I’m using employment_id as the primary key?
  • Would help if I deleted the ‘id’ field?
  • Is there any other best practice you would recommend?

Thanks for your help!

Derek.

Edit:

Adding migration file as requested:

class CreateEmployees < ActiveRecord::Migration
  def change
    create_table :employees do |t|
      t.string :employment_id, :unique => true
      etc...
    end
  end
end

EDIT

Very kind of nobody to point this out, but it’s clear to me now that the real answer is “Just because you can, doesn’t mean you should!”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:46:03+00:00Added an answer on June 8, 2026 at 7:46 am

    According to this post, it should solve the problem:

    class CreateEmployees < ActiveRecord::Migration
      def change
        create_table :employees, {:id => false} do |t|
          t.string :employment_id, :unique => true
          etc...
        end
      end
      execute "ALTER TABLE employees ADD PRIMARY KEY (employment_id);"
    end
    

    Also in your model:

    class Employee < ActiveRecord::Base
      set_primary_key :employment_id
      ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked before, but no one has given any
I know this question has been asked before and I have seen a plethora
I'm aware that variants of this question have been asked before, but I do
This question may have been asked before, but I couldn't find an answer to
I have asked this question before and got no answer so i am adding
I'm using the accepted answer to this question to help me build an HTML
I know this question has been asked before, but none of the answers in
Ok, first off, I applogize if this question has been asked before, I've spent
Sorry if this question has been asked 100 times before, unfortunately, i haven't found
I'm sorry if this question has been asked before, but I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.