This project is archived and is in readonly mode.
Oracle + join model, primary key issue
Reported by Greg Donald | November 4th, 2008 @ 07:08 PM | in 2.x
I'm using
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
in my current project.
My problem is that this setting seems to be causing me some issues with my join model's primary keys.
For example I have these three models:
class User < ActiveRecord::Base has_many :user_ugroups has_many :ugroups, :through => :user_ugroups end
class Ugroup < ActiveRecord::Base has_many :user_ugroups has_many :users, :through => :user_ugroups end
class UserUgroup < ActiveRecord::Base belongs_to :user belongs_to :ugroup end
My migration for the join table looks like this:
def self.up create_table :user_ugroups, :primary_key => :user_ugroups_id do |t| t.integer :user_id, :null => false t.integer :ugroup_id, :null => false end end
So when I create a UserUgroup like so:
UserUgroup.create!( :user => @gdonald, :ugroup => @admin )
ActiveRecord tries to insert a NULL for the user_ugroups_id field and then I get this:
ActiveRecord::StatementInvalid: OCIError: ORA-01400: cannot insert NULL into ("MYPROJDB"."USER_UGROUPS"."USER_UGROUPS_ID"): INSERT INTO user_ugroups (user_ugroups_id, user_id, ugroup_id) VALUES(NULL, 1, 1)
The migration creates my USER_UGROUPS_SEQ without issue, but then it doesn't get used.
I found a work-around for the issue:
before_create :get_id
def get_id self.ugroup_permission_id = connection.next_sequence_value( self.class.sequence_name ) end
Comments and changes to this ticket
-
Pratik March 12th, 2009 @ 04:04 PM
- State changed from new to invalid
This should probably be fixed by the Oracle adapter, which is not maintained in/by the rails core.
Thanks!
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>