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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:06:09+00:00 2026-05-30T06:06:09+00:00

Background: This is for a Ruby on Rails web application. I have a background

  • 0

Background: This is for a Ruby on Rails web application. I have a background job which downloads recent posts from Facebook and inserts them into the database. I’m using hand-coded SQL for performance. The RDBMS is PostgreSQL (on Heroku).

The table is called “posts”. I have a unique index on the combination of posts.uid and posts.contact_id. In the SQL, I use a WHERE condition to filter out uid–contact_id combinations which are already in the table, but even so, I am getting the following error:

ActiveRecord::RecordNotUnique: PGError: ERROR: duplicate key value violates unique constraint "index_posts_on_uid_and_contact_id"

Without further ado, here is the (dynamic) SQL:

INSERT INTO posts 
(message,contact_id,date,uid,created_at,updated_at,source,is_event)
SELECT  
    t.msg,
    contacts.id, 
    t.date,
    t.uid,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP,'facebook',
    FALSE
FROM contacts, 
(VALUES #{posts.map { |post| "(E'#{post['message'].escape_singles}','#
 {post['uid']}',DATE '#{format_date(post['time'])}',#{post['status_id']})" }.join(", ")}) AS      
 t (msg,fb_id,date,uid)
WHERE contacts.fb_id = t.fb_id 
AND (NOT EXISTS (
           SELECT * FROM posts 
           WHERE posts.uid = t.uid 
           AND posts.contact_id = contacts.id));

Shouldn’t the NOT EXISTS condition prevent this from happening?

  • 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-05-30T06:06:10+00:00Added an answer on May 30, 2026 at 6:06 am

    Your select query is returning duplicate rows.

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

Sidebar

Related Questions

Background - I have a web application for which a request takes several seconds.
I have a Ruby on Rails app that needs process many background jobs simultaneously:
Firstly, I'm not using rails. This is vanilla ruby application. I've read about packaging
I have the Ruby on Rails-based application Redmine (based on the BitNami redmine package)
I'm new to the rails environment and come from a java enterprise web application
Background: I am using the RailsInstaller 2 package from Engine Yard, which means Ruby
Background I'm running a Ruby on Rails application that has to serve a lot
I'm diving into ASP.NET MVC and I'm coming from a Ruby on Rails background.
Background This question is in two parts. I have a one-way WCF operation hosted
I have this background, the css code is: body { background: #FFF url('images/bg.png') no-repeat

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.