Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rails specify primary key id manually in the insert statement?

I am using the rails has_many_belongs_to_many association as follows:

class MemberRole < ActiveRecord::Base
  attr_accessible :org_id, :name, :permission_ids
  has_and_belongs_to_many :permissions
end

class Permission < ActiveRecord::Base
  has_and_belongs_to_many :member_roles
end

My join table is as follows:

CREATE TABLE MEMBER_ROLES_PERMISSIONS
( member_role_id NUMBER(38,0) NOT NULL REFERENCES member_roles,
  permission_id NUMBER(38,0) NOT NULL REFERENCES permissions
);

When I try to create a new record as follows:

role = MemberRole.create(name: role_params["name"], org_id: role_params["org_id"], permission_ids: role_params["permission_ids"])

INSERT INTO "MEMBER_ROLES" ("ID", "NAME", "ORG_ID") VALUES (:a1, :a2, :a3) [["id", 66], ["name", "test100"], ["org_id", "2"]]

INSERT INTO "MEMBER_ROLES_PERMISSIONS" ("MEMBER_ROLE_ID", "PERMISSION_ID") VALUES (66, 8)

The problem with the above approach is that my member_roles table has sequence and trigger created as follows:

CREATE SEQUENCE MEMBER_ROLES_SEQ;

set define off;
CREATE OR REPLACE TRIGGER member_roles_bir 
BEFORE INSERT ON MEMBER_ROLES 
FOR EACH ROW

BEGIN
  SELECT MEMBER_ROLES_SEQ.NEXTVAL
  INTO   :new.id
  FROM   dual;
END;

Because of the above the "ID" inserted inside the table is 67 instead of 66.

Should rails be even trying to insert the id manually? Is there a way I can tell rails not handle the id insert and let the oracle trigger handle it?

like image 530
Micheal Avatar asked Oct 31 '22 08:10

Micheal


1 Answers

This is because of the way oracle enhanced adapter works. Check these lines of code This is used to fetch the value from the sequence, so i assume Active record will be specifically sending this with the create statement and hence ID gets displayed in the query log.

like image 121
AshwinKumarS Avatar answered Nov 08 '22 14:11

AshwinKumarS