“SERIAL"处或附近的语法错误;仅使用自动增量

我在构建时遇到错误:

服务器因错误而无法启动:SequelizeDatabaseError: syntaxSERIAL"处或附近的错误

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

仅当参数 autoIncrement=true 被赋予主键时才会出现此错误.

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'
    });
}

我有 Sequelize 3.30.4 和 postgreSQL 9.6.

I have Sequelize 3.30.4 and postgreSQL 9.6.

我希望 autoIncrement 为 true,因为我正在使用 postgreSQL uuid_generate_v4() 生成 UUID.

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

推荐答案

这里不是常规的 sequelize 用户,但让我指出,在 postgreql 中对非顺序列使用 autoIncrement 不是正确的方法.Postgresql 不提供默认的 uuid 编号生成器,但可以轻松添加扩展 https://www.postgresql.org/docs/9.4/static/uuid-ossp.html.我相信你已经这样做了.

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.

下一步就是向我们提供 sequelize.fn 函数.

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

创建一个表示数据库函数的对象.这个可以用在搜索查询中,在 where 和 order 部分中,默认情况下列定义中的值.

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.

所以我们有

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

相关文章