Navigation Menu

Skip to content

Commit

Permalink
fix(oracle): reordered check constraint for unsigned numeric type (#1…
Browse files Browse the repository at this point in the history
…6074)

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
hjamil-24 and WikiRik committed Jun 1, 2023
1 parent fd38e79 commit 5c8250e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/dialects/oracle/query-generator.js
Expand Up @@ -887,32 +887,31 @@ export class OracleQueryGenerator extends AbstractQueryGenerator {
let unsignedTemplate = '';
if (attribute.type._unsigned) {
attribute.type._unsigned = false;
unsignedTemplate += ` check(${this.quoteIdentifier(attribute.field)} >= 0)`;
unsignedTemplate += ` check(${this.quoteIdentifier(options.attributeName)} >= 0)`;
}
template = attribute.type.toString();
template += unsignedTemplate;
} else {
template = '';
}


// Blobs/texts cannot have a defaultValue
if (
attribute.type &&
attribute.type !== 'TEXT' &&
attribute.type._binary !== true &&
Utils.defaultValueSchemable(attribute.defaultValue)
) {
template += ` DEFAULT ${this.escape(attribute.defaultValue)}`;
}
// Blobs/texts cannot have a defaultValue
if (
attribute.type &&
attribute.type !== 'TEXT' &&
attribute.type._binary !== true &&
Utils.defaultValueSchemable(attribute.defaultValue)
) {
template += ` DEFAULT ${this.escape(attribute.defaultValue)}`;
}

if (!attribute.autoIncrement) {
// If autoincrement, not null is setted automatically
if (attribute.allowNull === false) {
template += ' NOT NULL';
} else if (!attribute.primaryKey && !Utils.defaultValueSchemable(attribute.defaultValue)) {
template += ' NULL';
if (!attribute.autoIncrement) {
// If autoincrement, not null is set automatically
if (attribute.allowNull === false) {
template += ' NOT NULL';
} else if (!attribute.primaryKey && !Utils.defaultValueSchemable(attribute.defaultValue)) {
template += ' NULL';
}
}
template += unsignedTemplate;
} else {
template = '';
}

if (attribute.unique === true && !attribute.primaryKey) {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/dialects/oracle/query-generator.test.js
@@ -0,0 +1,31 @@

'use strict';

const Support = require('../../support'),
DataTypes = require('sequelize/lib/data-types'),
expectsql = Support.expectsql,
current = Support.sequelize,
sql = current.dialect.queryGenerator;

if (current.dialect.name === 'oracle') {
describe('createTable', () => {
const FooUser = current.define('user', {
intCol: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
defaultValue: 0
}
},
{
schema: 'foo',
timestamps: false
});

describe('[Oracle Specific] QueryGenerator', () => {
it('checks for values >=0', () => {
expectsql(sql.createTableQuery(FooUser.getTableName(), sql.attributesToSQL(FooUser.rawAttributes), { }), {
default: 'BEGIN EXECUTE IMMEDIATE \'CREATE TABLE "foo"."users" ("id" NUMBER(*,0) GENERATED BY DEFAULT ON NULL AS IDENTITY, "intCol" INTEGER DEFAULT 0 NOT NULL check("intCol" >= 0),PRIMARY KEY ("id"))\'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -955 THEN RAISE; END IF; END;' });
});
});
});
}

0 comments on commit 5c8250e

Please sign in to comment.