Forum: Ruby on Rails Auto increment primary key different from the default 'id'

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2f638d0303b58764e3307101546bf0d9?d=identicon&s=25 Marc (Guest)
on 2009-04-02 13:58
(Received via mailing list)

I working in an application (developed in access over a oracle DB) on
production server with all tables and fields that can't be changed. No
table has an 'id' field for his primary key. I want to migrate this
application to RoR, first in a development server, but i found some

I run a scaffold to create one simple table, and this is my migration
class CreateSafCarrecs < ActiveRecord::Migration
  def self.up
    create_table (:saf_carrecs, :primary_key => "idcarrec") do |t|
      t.column :desccarrec, :text
  def self.down
    drop_table :saf_carrecs

With this code, and running 'rake db:migrate', the table and the
sequence for the autonumeric primary_key was created at the database.
But when i want to insert a new row in the table through the new form
generated with scaffold, it shows me this error:

OCIError: ORA-01400: cannot insert NULL into
("SAF_CARRECS"."IDCARREC"): INSERT INTO saf_carrecs (idcarrec,
desccarrec) VALUES(NULL, empty_clob())

It seems like the primary_key was not generated automaticly.

I search for a solution and i modify my model like this:
class SafCarrec < ActiveRecord::Base
  before_create :get_next_id
  def get_next_id
    id = ActiveRecord::Base.connection.select_value("select
saf_carrecs_seq.nextval from dual")
    self.idcarrec = id

With this modification I consult the development.log and seems like
the insert goes ok:
'INSERT INTO saf_carrecs (idcarrec, desccarrec) VALUES(10080,

But just after this line in development.log, RoR make a select of the
row by the 'id' field and show me this error in the browser:
OCIError: ORA-00904: "ID": invalid identifier: SELECT desccarrec FROM
saf_carrecs WHERE id = 10081

How can RoR make a select using 'id' field, if I changed it for
'idcarrec'? How can make it works?

Thanks, and sorry for my English.
359b7a7fc263382baee62d91c1e4c25e?d=identicon&s=25 Emanuele Tozzato (mekdigital)
on 2009-04-02 14:14
(Received via mailing list)
Your model should have:
:primary_key => 'idcarrec' for AR to know how to build SQL.

2f638d0303b58764e3307101546bf0d9?d=identicon&s=25 Marc (Guest)
on 2009-04-03 08:26
(Received via mailing list)
Thank you so much emanuele!

I put 'set_primary_key "idcarrec"' in my saf_carrec.rb and all works
This topic is locked and can not be replied to.