Skip to content

SMALLINT Data Type fails with postgres when autoIncrement = true #9397

Closed

Description

@mjy78

What are you doing?

Creating a table with a SMALLINT field that has autoIncrement enabled using Postgres

sequelize.define('Model', {
    testField: {
      field: 'test_field_id',
      type: DataTypes.SMALLINT,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    })

What do you expect to happen?

Table should be generated when running sequelize.

What is actually happening?

Table creation fails with syntax error syntax error at or near "SERIAL"

Dialect: postgres
Database version: 9.6
Sequelize version: 4.37.6
Tested with latest release: Yes

The query generator for postgres (~/lib/dialects/postgres/query-generator.js) has specific handling for BIGINT with autoIncrement, but not SMALLINT.

To resolve the issue I believe we need to change

    if (_.includes(dataType, 'SERIAL')) {
      if (_.includes(dataType, 'BIGINT')) {
        dataType = dataType.replace(/SERIAL/, 'BIGSERIAL');
        dataType = dataType.replace(/BIGINT/, '');
      } else {
        dataType = dataType.replace(/INTEGER/, '');
      }
      dataType = dataType.replace(/NOT NULL/, '');
    }

to this

    if (_.includes(dataType, 'SERIAL')) {
      if (_.includes(dataType, 'BIGINT')) {
        dataType = dataType.replace(/SERIAL/, 'BIGSERIAL');
        dataType = dataType.replace(/BIGINT/, '');
      } else if (_.includes(dataType, 'SMALLINT')) {
          dataType = dataType.replace(/SERIAL/, 'SMALLSERIAL');
          dataType = dataType.replace(/SMALLINT/, '');        
      } else {
        dataType = dataType.replace(/INTEGER/, '');
      }
      dataType = dataType.replace(/NOT NULL/, '');
    }

Activity

  1. added
    type: bug
    good first issue
    on May 5, 2018
  2. sushantdhiman commented on May 8, 2018

    @sushantdhiman
    Contributor

    Fixed by #9406

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuetype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions