This project is archived and is in readonly mode.

#1814 ✓stale
deepak

unable to create a model entity with a custom primary key

Reported by deepak | January 29th, 2009 @ 01:48 AM | in 3.x

Hi All, I ran into the following issue today, hope this is the right channel for it. I searched both Lighthouse & Trac and haven't found anything similar.

I'm trying to use a custom primary key. In this case, I'm creating a domain data table for countries. The primary key is the 2 digit ISO 3166 code. Here's my Migration :


class CreateCountries < ActiveRecord::Migration
  def self.up
    create_table :countries, :id => false do |t|
      t.string :country_code, :limit => 2, :null => false
      t.string :name, :null => false

      t.timestamps
    end
    
    add_index :countries, :country_code, :unique => true
  end

  def self.down
    drop_table :countries
  end
end

The database is MySQL (See the end of the ticket for platform versions). The migration runs fine, and creates the right table definition :



mysql> describe countries;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| country_code | varchar(2)   | NO   | PRI | NULL    |       | 
| name         | varchar(255) | NO   |     | NULL    |       | 
| created_at   | datetime     | YES  |     | NULL    |       | 
| updated_at   | datetime     | YES  |     | NULL    |       | 
+--------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Here is my model definition (I've tried both the literal 'country_code' and :country_code with the same results) :


class Country < ActiveRecord::Base
  set_primary_key :country_code
end

Now, here's the unexpected behavior :


$> script/console
Loading development environment (Rails 2.2.2)
>> Country.create(:country_code => "US", :name => "United States")
=> #<Country country_code: 0, name: "United States", created_at: "2009-01-29 01:22:48", updated_at: "2009-01-29 01:22:48">

Here's what is in the log. At the risk of pointing out the obvious, in addition to the warning, the SQL being sent to the DB is missing the country_code.


WARNING: Can't mass-assign these protected attributes: country_code
  SQL (0.2ms)   BEGIN
  Country Create (0.3ms)   INSERT INTO `countries` (`name`, `updated_at`, `created_at`) VALUES('United States', '2009-01-29 01:27:57', '2009-01-29 01:27:57')
  SQL (0.8ms)   COMMIT

If I instead use new & save, things work as expected. Environment Rails : 2.2.2 Ruby : ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-darwin9.5.0] MySQL : mysql5 Ver 14.12 Distrib 5.0.67, for apple-darwin9.5.0 (i686) using EditLine wrapper OS : OSX 10.5.6

Comments and changes to this ticket

  • deepak

    deepak January 29th, 2009 @ 01:53 AM

    Hmm, the formatting seems to have been lost at the end. Here are my environment settings again :

    Environment

    • Rails : 2.2.2
    • Ruby : ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-darwin9.5.0]
    • MySQL : mysql5 Ver 14.12 Distrib 5.0.67, for apple-darwin9.5.0 (i686) using EditLine wrapper
    • OS : OSX 10.5.6
  • Jeff

    Jeff April 5th, 2009 @ 09:30 PM

    It appears you can't create a model with a specific primary key value regardless of whether it is custom or default. The code you can preventing it is here: http://github.com/rails/rails/bl...

    Essentially when creating the object it looks to see what attributes are assignable in attributes_from_column_definition, and the primary_key is excluded there.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer November 1st, 2010 @ 05:07 PM

    • Tag cleared.
    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:20 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:20 PM

    • State changed from “open” to “stale”
  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages