Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gavvvr opened this issue Sep 14, 2020 · 2 comments
Open

Comments

@gavvvr
Copy link
Contributor

gavvvr commented Sep 14, 2020

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

@gavvvr gavvvr changed the title How to create records into table with 'generated always as identity' primary key? How to create records into table with generated 'as identity' primary key? Sep 14, 2020
@stale
Copy link

stale bot commented Dec 25, 2020

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.

@stale stale bot added the wontfix label Dec 25, 2020
@yahonda yahonda removed the wontfix label Dec 29, 2020
@yahonda
Copy link
Collaborator

yahonda commented Apr 22, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants