diff --git a/src/dialects/oracle/query-generator.js b/src/dialects/oracle/query-generator.js index ee1b5775af2b..e4ca7e6f729a 100644 --- a/src/dialects/oracle/query-generator.js +++ b/src/dialects/oracle/query-generator.js @@ -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) { diff --git a/test/unit/dialects/oracle/query-generator.test.js b/test/unit/dialects/oracle/query-generator.test.js new file mode 100644 index 000000000000..a6750d18a4bb --- /dev/null +++ b/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;' }); + }); + }); + }); +} \ No newline at end of file