我只使用ActiveRecord进行遗留Oracle数据库的某些开发。我的适配器是activerecord-oracle_enhanced-adapter(https://github.com/rsim/oracle-enhanced)。所以我不想AR处理主键生成。如何禁用序列生成的主键?
class User < Activied::Base
self.table_name = "users"
self.primary_key = "user_id"
end
user = User.new
user.save
然后我收到了错误:
stmt.c:230:in oci8lib_191.so: ORA-02289: sequence does not exist (OCIError)
当我将代码更改为
时class User < ActiveRecord::Base
self.table_name = 'users'
self.primary_key = "user_id"
self.sequence_name = nil
end
我收到了另一个错误:
stmt.c:230:in oci8lib_191.so: ORA-00936: missing expression (OCIError)
那么有没有人知道如何手动管理主键?我只是想做一些简单的插入。
由于
答案 0 :(得分:2)
也许现在为时已晚,但最终我找到了回答。不久:
self.sequence_name = :autogenerated
来自源代码评论here:
使用说明:
# * Key generation assumes a "${table_name}_seq" sequence is available # for all tables; the sequence name can be changed using # ActiveRecord::Base.set_sequence_name. When using Migrations, these # sequences are created automatically. # ***Use set_sequence_name :autogenerated **** with legacy tables that have # triggers that populate primary keys automatically.**