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 8508557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:11:37+00:00 2026-06-11T03:11:37+00:00

Problem creating new records in postgres database. Using Heroku Cedar stack and Rails 3.2.

  • 0

Problem creating new records in postgres database. Using Heroku Cedar stack and Rails 3.2.

Calling create results in

ActiveRecord::RecordNotUnique (PG::Error: ERROR:  duplicate key value violates unique constraint "answer_selections_pkey"

Here’s my schema for the problem table (it should be using the implicit auto-increment id column):

  create_table "answer_selections", :force => true do |t|                                                              
    t.integer "individual_id"
    t.integer "answer_choice_id"
    t.boolean "still_true",       :default => false
  end

And here’s the error when calling create.

2012-09-07T14:46:44+00:00 app[web.1]: ActiveRecord::RecordNotUnique (PG::Error: ERROR:  duplicate key value violates unique constraint "answer_selections_pkey"
2012-09-07T14:46:44+00:00 app[web.1]: : INSERT INTO "answer_selections" ("answer_choice_id", "individual_id", "still_true") VALUES ($1, $2, $3) RETURNING "id"):
2012-09-07T14:46:44+00:00 app[web.1]:   app/controllers/answer_selections_controller.rb:10:in `create'

Similarly, I see the same error on another table:

2012-09-09T04:10:34+00:00 app[web.2]: ActiveRecord::RecordNotUnique (PG::Error: ERROR:  duplicate key value violates unique constraint "trips_pkey"
2012-09-09T04:10:34+00:00 app[web.2]: : INSERT INTO "trips" ("booked", "created_at", "destination_id", "end_date", "individual_id", "n_travelers", "name", "start_date", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"):

Unable to access postgres database directly, as the connection times out:

$ heroku pg:psql
psql: could not connect to server: Connection timed out

How can I repair this, and how can I avoid it in the future? I have read that running VACCUUM and ANALYZE will solve it, but as I said I cannot access psql.

Edit to add requested info (sample schema included above, we’re using ActiveRecord):

@record.create(params) # How these records are created
  • 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-11T03:11:39+00:00Added an answer on June 11, 2026 at 3:11 am

    I’m not sure what happened to your data but somehow the sequence that supplies id values has gotten out of sync with the table’s id values; this can happen if you supply an explicit id value in an INSERT like this:

    insert into t (id, col) values (11, 'pancakes')
    

    Perhaps you’ve loaded some bulk data but didn’t kick your sequences back into sense.

    In any case, a simple migration can sort you out:

    class KickAnswerSelectionSequence < ActiveRecord::Migration
        def up
            connection.execute(%q{
                select setval('answer_selections_id_seq', max(id))
                from answer_selections
            })
        end
        def down
            raise ActiveRecord::IrreversibleMigration
        end
    end
    

    You’ll want to repeat that for all the tables that are giving you problems, might be a good idea to do it for all your tables just to be safe.

    You can read more on sequence manipulation functions if desired:

    http://www.postgresql.org/docs/current/interactive/functions-sequence.html

    As to your problem with heroku pg:psql, you’ll have to talk to Heroku about that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I'm creating a new XML File out of a table. The problem is I
I have a problem creating the following SQL Statement using LINQ & C# select
I'm having a problem creating a trigger on a database for a project I've
I was trying to create Hibernate Validator bean, and run into this problem creating
I am new to python and I am using django for student database application
I made the username as PRIMARY KEY in the database. Creating new account with
Rails 3.2.5, Devise 2.1 Using Devise to authenticate Users, and am having a problem
I've created a plugin to create new records when a field changes on another
I am having a problem creating a WCF service instance with a parameter. The

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.