Hi! 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 problems: I run a scaffold to create one simple table, and this is my migration code: class CreateSafCarrecs < ActiveRecord::Migration def self.up create_table (:saf_carrecs, :primary_key => "idcarrec") do |t| t.column :desccarrec, :text end end def self.down drop_table :saf_carrecs end end 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 end end With this modification I consult the development.log and seems like the insert goes ok: ''INSERT INTO saf_carrecs (idcarrec, desccarrec) VALUES(10080, empty_clob())'' 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
emanuele tozzato
2009-Apr-02 12:12 UTC
Re: Auto increment primary key different from the default ''id''
Your model should have: :primary_key => ''idcarrec'' for AR to know how to build SQL. iPhonized! On Apr 2, 2009, at 4:19 AM, Marc <jump.rules-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi! > > 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 > problems: > > I run a scaffold to create one simple table, and this is my migration > code: > class CreateSafCarrecs < ActiveRecord::Migration > def self.up > create_table (:saf_carrecs, :primary_key => "idcarrec") do |t| > t.column :desccarrec, :text > end > end > def self.down > drop_table :saf_carrecs > end > end > > 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 > end > end > > With this modification I consult the development.log and seems like > the insert goes ok: > ''INSERT INTO saf_carrecs (idcarrec, desccarrec) VALUES(10080, > empty_clob())'' > > 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. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marc
2009-Apr-03 06:25 UTC
Re: Auto increment primary key different from the default ''id''
Thank you so much emanuele! I put ''set_primary_key "idcarrec"'' in my saf_carrec.rb and all works fine. On 2 abr, 14:12, emanuele tozzato <etozz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Your model should have: > :primary_key=> ''idcarrec'' for AR to know how to build SQL. > > iPhonized! > > On Apr 2, 2009, at 4:19 AM, Marc <jump.ru...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi! > > > 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 > > problems: > > > I run a scaffold to create one simple table, and this is my migration > > code: > > class CreateSafCarrecs < ActiveRecord::Migration > > def self.up > > create_table (:saf_carrecs, :primary_key=> "idcarrec") do |t| > > t.column :desccarrec, :text > > end > > end > > def self.down > > drop_table :saf_carrecs > > end > > end > > > With this code, and running ''rake db:migrate'', the table and the > > sequence for theautonumericprimary_keywas 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 theprimary_keywas 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 > > end > > end > > > With this modification I consult the development.log and seems like > > the insert goes ok: > > ''INSERT INTO saf_carrecs (idcarrec, desccarrec) VALUES(10080, > > empty_clob())'' > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---