rsim / oracle-enhanced

Oracle enhaced adapter for ActiveRecord
MIT License
548 stars 308 forks source link

How to create records into table with generated 'as identity' primary key? #2049

Open gavvvr opened 4 years ago

gavvvr commented 4 years ago

Steps to reproduce

So I have Oracle v12 which means I can use as identity in column definition to get autogenerated id. I have the following table:

create table t_text_file
(
    text_file_id number(12) generated always as identity
        constraint t_text_file_pk
            primary key,
    name         varchar2(20),
    data         clob
)

My model is:

module Database
  class TextFile < ActiveRecord::Base
    self.table_name = 't_text_file'
    self.primary_key = 'text_file_id'
    # self.sequence_name = :autogenerated
  end
end

The code I execute is:

Database::TextFile.create(name: 'file.csv', data: "line1\nline2")

Expected behavior

Database insert without errors

Actual behavior

I get the following error:

OCIError: ORA-02289: sequence does not exist

What is going on in logs at the same time:

D, [2020-09-14T19:17:12.205496 #52369] DEBUG -- : next sequence value (113.6ms) SELECT "T_TEXT_FILE_SEQ".NEXTVAL FROM dual

It tries guess sequence name T_TEXT_FILE_SEQ and use it.

If I uncomment self.sequence_name = :autogenerated, the error is:

ArgumentError: Trigger based primary key is not supported

Obviously I am not trying to use trigger. I just want to disable using sequence and rely on a new mechanism of using Identity available from Oracle 12. I see the change comes from #1669, :autogenerated can not be used anymore since 6.x. But how am I supped to use as identity columns? self.sequence_name = :autogenerated will not work too and will lead to null pointer error.

System configuration

I use ActiveRecord without Rails:

gem 'activerecord', '~> 6.0.3', :require => "active_record"

Oracle enhanced adapter version:

gem 'activerecord-oracle_enhanced-adapter', '~> 6.0.4'

Ruby version:

2.6.6

Oracle Database version:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

yahonda commented 3 years ago

As of right now, Oracle enhanced adapter does not support identity data type then it should not work. There was a plan to support it but did not have enough time/motivation to implement it.