Skip to content
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

Fix autoincrement for Oracle with schema #4594

Merged
merged 3 commits into from Jul 29, 2021
Merged

Conversation

kibertoad
Copy link
Collaborator

fixes #4592

@kibertoad
Copy link
Collaborator Author

kibertoad commented Jul 29, 2021

@code-ape I don't have an Oracle instance at hand to test, could you check if this thing actually works? If not, would appreciate suggestions on how expected SQL output should actually look.

@code-ape
Copy link
Collaborator

@kibertoad will check immediately and report back! Thank you.

@code-ape
Copy link
Collaborator

Found that we needed to match cons.owner on the given schema and also create the trigger and sequence in the given sequence. Code may need a bit of cleaning, but appears to work!

View code
createAutoIncrementTrigger: function (logger, tableName, schemaName) {
    const tableQuoted = `"${tableName}"`;
    const tableUnquoted = tableName;
    const schemaQuoted = schemaName ? `"${schemaName}".` : '';
    const constraintOwner = schemaName ? `'${schemaName}'` : 'cols.owner';
    const triggerName = utils.generateCombinedName(
      logger,
      'autoinc_trg',
      tableName
    );
    const sequenceNameUnQuoted = utils.generateCombinedName(logger, 'seq', tableName);
    const sequenceNameQuoted = `"${sequenceNameUnQuoted}"`
    return (
      `DECLARE ` +
      `PK_NAME VARCHAR(200); ` +
      `BEGIN` +
      `  EXECUTE IMMEDIATE ('CREATE SEQUENCE ${schemaQuoted}${sequenceNameQuoted}');` +
      `  SELECT cols.column_name INTO PK_NAME` +
      `  FROM all_constraints cons, all_cons_columns cols` +
      `  WHERE cons.constraint_type = 'P'` +
      `  AND cons.constraint_name = cols.constraint_name` +
      `  AND cons.owner = ${constraintOwner}` +
      `  AND cols.table_name = '${tableUnquoted}';` +
      `  execute immediate ('create or replace trigger ${schemaQuoted}"${triggerName}"` +
      `  BEFORE INSERT on ${schemaQuoted}${tableQuoted}` +
      `  for each row` +
      `  declare` +
      `  checking number := 1;` +
      `  begin` +
      `    if (:new."' || PK_NAME || '" is null) then` +
      `      while checking >= 1 loop` +
      `        select ${schemaQuoted}${sequenceNameQuoted}.nextval into :new."' || PK_NAME || '" from dual;` +
      `        select count("' || PK_NAME || '") into checking from ${schemaQuoted}${tableQuoted}` +
      `        where "' || PK_NAME || '" = :new."' || PK_NAME || '";` +
      `      end loop;` +
      `    end if;` +
      `  end;'); ` +
      `END;`
    );
  }

@kibertoad kibertoad merged commit 124107e into master Jul 29, 2021
@kibertoad kibertoad deleted the fix/4592-oracle-autoinc branch July 29, 2021 14:07
OlivierCavadenti pushed a commit to AbeonaPascha/knex that referenced this pull request Nov 4, 2021
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

Successfully merging this pull request may close these issues.

table.increments() errors on Oracle when .withSchema()
2 participants