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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:53:21+00:00 2026-06-04T23:53:21+00:00

I’ve got a Rails 3 app that I’m working on. I’m using the composite_primary_keys

  • 0

I’ve got a Rails 3 app that I’m working on. I’m using the composite_primary_keys gem for a few tables, but Rails is still creating an id field that’s not being used (i.e. it’s nil for each entry). While it runs on my local machine in SQLite3, I can’t run the app on Heroku. Postgresql throws a fit at me and gives me this error:

2012-05-31T21:12:36+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR:  null value in column "id" violates not-null constraint
2012-05-31T21:12:36+00:00 app[web.1]:   app/controllers/items_controller.rb:57:in `block (2 levels) in create'
2012-05-31T21:12:36+00:00 app[web.1]: : INSERT INTO "item_attr_quants" ("attribute_id", "created_at", "id", "item_id", "updated_at", "value") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "item_id","attribute_id"):

Since the “id” field is nil, Postgresql’s yelling at me.

Is there a way I can prevent the “id” field from being created in the first place, delete the column using a raw SQL statement, force Postgresql on Heroku to allow the “id” field to be null, or get around this some other way? I’m dead-set on using composite primary keys, so I don’t want to delete the gem and rewrite code.

Model

class ItemAttrQuant < ActiveRecord::Base
  belongs_to :item
  belongs_to :attribute
  self.primary_keys = :item_id, :attribute_id
end

Migration

class CreateItemAttrQuants < ActiveRecord::Migration
  def change
    create_table :item_attr_quants do |t|
      t.belongs_to :item
      t.belongs_to :attribute
      t.integer :value

      t.timestamps
    end
    add_index :item_attr_quants, :item_id
    add_index :item_attr_quants, :attribute_id
  end
end
  • 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-04T23:53:22+00:00Added an answer on June 4, 2026 at 11:53 pm

    You can use the :id => false and :primary_key options to create_table in your migration:

    class CreateItemAttrQuants < ActiveRecord::Migration
      def change
        create_table :item_attr_quants, :id => false do |t|
          ...
        end
        ...
      end
    end
    

    That will create item_attr_quants without the id column but your table won’t have a real primary key. You could add fake one by specifying not null for item_id and attribute_id and adding a unique index on those two columns:

    class CreateItemAttrQuants < ActiveRecord::Migration
      def change
        create_table :item_attr_quants, :id => false do |t|
          t.integer :item_id, :null => false
          t.integer :attribute_id, :null => false
          t.integer :value
          t.timestamps
        end
        add_index :item_attr_quants, [:item_id, :attribute_id], :unique => true
        add_index :item_attr_quants, :item_id
        add_index :item_attr_quants, :attribute_id
      end
    end
    

    I don’t think ActiveRecord fully understands the notion of a real composite primary key inside the database so a unique index is AFAIK the best you can do unless you want to manually send an ALTER TABLE into the database.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.