最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - syntax error at or near "SERIAL" with autoIncrement only - Stack Overflow

programmeradmin3浏览0评论

I get the error on build :

Server failed to start due to error: SequelizeDatabaseError: syntax error at or near "SERIAL"

This error ONLY appears when the parameter autoIncrement=true is given to the primary key.

'use strict';

export default function(sequelize, DataTypes) {
  return sequelize.define('Ladder', {
    ladder_id: {
      type: DataTypes.UUID,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true //<------- If commented it works fine
    },
    ladder_name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true
    },
    ladder_description: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    ladder_open: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    },
    ladder_hidden: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    },
    ladder_creation_date: {
      type: DataTypes.DATE,
      allowNull: false
    },
    ladder_fk_user: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    ladder_fk_game: {
      type: DataTypes.UUID,
      allowNull: false
    },
    ladder_fk_platforms: {
      type: DataTypes.ARRAY(DataTypes.UUID),
      allowNull: false
    }

  },
    {
      schema: 'ladder',
      tableName: 'ladders'
    });
}

I have Sequelize 3.30.4 and postgreSQL 9.6.

I want autoIncrement at true because I am generating the UUID with postgreSQL uuid_generate_v4().

I get the error on build :

Server failed to start due to error: SequelizeDatabaseError: syntax error at or near "SERIAL"

This error ONLY appears when the parameter autoIncrement=true is given to the primary key.

'use strict';

export default function(sequelize, DataTypes) {
  return sequelize.define('Ladder', {
    ladder_id: {
      type: DataTypes.UUID,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true //<------- If commented it works fine
    },
    ladder_name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true
    },
    ladder_description: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    ladder_open: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    },
    ladder_hidden: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    },
    ladder_creation_date: {
      type: DataTypes.DATE,
      allowNull: false
    },
    ladder_fk_user: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    ladder_fk_game: {
      type: DataTypes.UUID,
      allowNull: false
    },
    ladder_fk_platforms: {
      type: DataTypes.ARRAY(DataTypes.UUID),
      allowNull: false
    }

  },
    {
      schema: 'ladder',
      tableName: 'ladders'
    });
}

I have Sequelize 3.30.4 and postgreSQL 9.6.

I want autoIncrement at true because I am generating the UUID with postgreSQL uuid_generate_v4().

Share Improve this question asked May 16, 2017 at 6:14 rXprXp 6972 gold badges12 silver badges27 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

Not a regular sequelize user here but let me point out that using autoIncrement for non sequential column is not the right way in postgreql. Postgresql does not provide a default uuid number generator but an extension can be added easily https://www.postgresql.org/docs/9.4/static/uuid-ossp.html. I believev you have already done so.

The next step then is to us the sequelize.fn function.

Creates an object representing a database function. This can be used in search queries, both in where and order parts, and as default values in column definitions.

so we have

ladder_id: {
    type: DataTypes.UUID,
    allowNull: false,
    primaryKey: true,
    default: sequelize.fn('uuid_generate_v4')
}

My guess is that autoIncrement for PostgreSQL is hardcoded to use SERIAL type for column and this conflicts with your choice of UUID.

Try removing autoincrement parameter and instead use defaultvalue:

return sequelize.define('Ladder', {
    ladder_id: {
      type: DataTypes.UUID,
      allowNull: false,
      primaryKey: true,
      defaultValue: UUIDV4
    },
child_id: {
    primaryKey: true,
    autoIncrement: true,
    type: Sequelize.INTEGER
},
parent_id: {
    references: {
        model: 'parent',
        key: 'parent_id'
    },
    type: Sequelize.INTEGER
}
发布评论

评论列表(0)

  1. 暂无评论