Auto increment primary key different from the default 'id'


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.

Your model should have:
:primary_key => ‘idcarrec’ for AR to know how to build SQL.


Thank you so much emanuele!

I put ‘set_primary_key “idcarrec”’ in my saf_carrec.rb and all works

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs