Skip to content

Error "type 'serial' does not exist" #13167

Description

@dalisoft

Issue Description

I getting error type "serial" does not exist

What are you doing?

// postgres.js
const sql = new Sequelize(db, username, password, {
  host,
  port,
  dialect: 'postgres',
  dialectOptions: {
    ssl: false
  },
  logging: false,

  define: {
    timestamps: false,
    charset: 'utf8'
  }
});

sql
  .sync({ alter: true })
  .catch((err) =>
    logger.error(
      `[PostgreSQL]: Unable to the sync tables from server;
      ${err.message}
      ${err.parent && err.parent.sql ? '; with these SQL Query' : ''}
      ${err.parent && err.parent.sql ? '\n' : ''}
      ${(err.parent && err.parent.sql) || ''}`
    )
  )
  .then(() => sql.authenticate())
  .then(() =>
    logger.info('[PostgreSQL]: Connection has been established successfully.')
  )
  .catch((err) =>
    logger.error(
      `[PostgreSQL]: Unable to connect to the database
      ${err.message}`
    )
  );

export default sql;

// the file error happening
import DataTypes from 'sequelize';
import { v4 as uuidv4 } from 'uuid';
import sequelize from '../connections/postgres.js';

export default sequelize.define(
  'Order',
  {
    id: {
      type: DataTypes.UUID,
      defaultValue() {
        return uuidv4();
      },
      primaryKey: true
    },
    seqNo: {
      type: DataTypes.INTEGER,
      autoIncrement: true
    },
    description: {
      type: DataTypes.STRING,
      allowNull: false
    }
  },
  {
    freezeTableName: true,
    // define the table's name
    tableName: 'orders'
  }
);

What do you expect to happen?

Successfully sync, altering table

What is actually happening?

[PostgreSQL]: Unable to the sync tables from server;
      type "serial" does not exist
      ; with these SQL Query
      

      ALTER TABLE "orders" ALTER COLUMN "seqNo" DROP NOT NULL;ALTER TABLE "orders" ALTER COLUMN "seqNo" DROP DEFAULT;ALTER TABLE "orders" ALTER COLUMN "seqNo" TYPE  SERIAL;
[PostgreSQL]: Connection has been established successfully.

Additional context

Add any other context or screenshots about the feature request here.

Environment

  • Sequelize version: v6.5.0
  • Node.js version: v15.13.0
  • Operating System: macOS Catalina 10.15.7
  • If TypeScript related: TypeScript version: XXX

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using Postgres, with [email protected] and [email protected] versions and database version postgres@12

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. Khuzha commented on Oct 14, 2021

    @Khuzha

    have the same issue

  2. dalisoft commented on Oct 17, 2021

    @dalisoft
    Author

    It's my code bug. autoIncrement should be either primary or unique to be error-free. May be another case triggered for another codebase

  3. github-actions commented on Oct 27, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  4. nziranzizacb commented on Mar 5, 2022

    @nziranzizacb

    Just facing this issue

  5. ephys commented on Mar 7, 2022

    @ephys
    Member

    Looks like SERIAL cannot be used in ALTER COLUMN, only in CREATE TABLE. Just tested with postgres 12

    We'd need to change it to

    CREATE SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
    
    ALTER TABLE tablename ALTER COLUMN colname TYPE INTEGER SET DEFAULT nextval('tablename_colname_seq');
  6. reopened this on Mar 7, 2022
  7. added
    dialect: postgres
    type: bug
    and removed on Mar 7, 2022
  8. Zaggermeister commented on Mar 29, 2022

    @Zaggermeister
  9. ephys commented on Mar 29, 2022

    @ephys
    Member

    Use the 👍 reaction on the initial post instead :) we can use it to sort by most requested issue

  10. john-easci commented on Jul 31, 2024

    @john-easci

    I just ran into this... somebody wanted a GUID for the key, but to have an autoincrement alongside to be human-readable. It worked in code, but the migration failed due to the issues above.

    So instead of waiting for a fix which may never happen, I added the following as a workaround:

        Item.beforeCreate(async (item, options) => {
            item.numericKey = ((await Item.max('numericKey')) as number) + 1;
        });
    

    Ugly because it requires another DB call (and you have to make sure your table is set up properly, while also creating a potential issue with deleted rows), but seems to have done the job for now, and maybe it helps the next guy who has this issue.

  11. added theissue type on Mar 9, 2025
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

    dialect: postgrestype: bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions