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(oracle): reordered check constraint for unsigned numeric type #16074

Merged
merged 3 commits into from Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;' });
});
});
});
}