Skip to content

Duplicate entry '...' for key 'PRIMARY' for long id field #7420

wafcio opened this Issue Aug 22, 2012 · 3 comments

3 participants

wafcio commented Aug 22, 2012

I need add to id values with 15 digits, it is id from facebook objects and it is not depent on me. First I changed int to string, it works in browser. But in test with capybara it not work, even if I drop, create and migrate test database once more. So next I will change string to integer but with limit 8 (I'm using mysql)

Instead of this, when I tried saved long number in integer I got duplicate entry error from mysql (strange).

But what is the problem when I changed id from int to string, in test (after load schema.rb file) id field in sql query was still like integer and schema.rb file not have any information about id type (string). And after changed id to integer with limit 8, schema.rb didn't have information about this too. It is very bad, because after migration id is big int but after load schema id is only integer

Summing up:
schema.rb are able to create primary key only as integer. it would be grate to support other datatype for primary key

Ruby on Rails member

I feel like this is a duplicate, but I just can't find the ticket. :(

Ruby on Rails member
senny commented Nov 11, 2012

I'm not sure if I understand the issue completely. You could try to define the primary key as follows:

class Article < ActiveRecord::Base
  self.primary_key = "string_pk"

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles, :id => false do |t|
      t.string :string_pk, :primary => true


If this does not help could you please provide a test-case or a sample app with your problem? This would help a lot to investigate the problem. Also, what version of rails do you use?

Ruby on Rails member
senny commented Jan 12, 2013

We havent heard from you in 2 months. Without further information we can't pursue the issue further. I'm closing this for now. Please reopen if the problem still occurs and post more information.

@senny senny closed this Jan 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.