"Sequel :: Error: id is a limited primary key" when creating a record using Sequel

I have a model based on the Sequel and Oracle adapter:

class Operation < Sequel::Model(DB[:operations]) end 

If I try to create a record using the Oracle.nextval sequence as the primary key:

 Operation.create( :id=>:nextval.qualify(:Soperations), :payee_id=>12345, :type=>"operation", :origin=>"user-12345", :parameters=>{}.to_s ) 

I have an error: Sequel::Error: id is a restricted primary key . What is the correct way to create a record in this case or "match" an Oracle sequence to an id column? Or maybe I need to use unrestrict_primary_key ?

+6
source share
2 answers

unrestrict_primary_key will allow you to assign mass to the primary key field. However, this is probably not what you want to do in this case, unless you also want to disable type casting. Since you just want to set the value to create, I recommend using before_create:

 class Operation def before_create values[:id] ||= :nextval.qualify(:Soperations) super end end 
+7
source

To just create an instance of new with the desired id , you can:

 Operation.new(attrs).tap { |o| o.id = id } 
+2
source

Source: https://habr.com/ru/post/908505/


All Articles