5

我只是将 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)

那么有没有人知道如何手动管理主键?我只想做一些简单的插入。

谢谢

4

1 回答 1

4

也许为时已晚,但最终我找到了答案。不久:

self.sequence_name = :autogenerated

从这里的源代码注释:

使用说明:

# * 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.**
于 2014-01-18T08:51:45.013 回答