rXp*_*rXp 5 javascript postgresql sequelize.js angularjs
我在构建时收到错误:
由于错误,服务器无法启动:SequelizeDatabaseError:“SERIAL”处或附近的语法错误
此错误仅在为主键提供参数 autoIncrement=true 时出现。
'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'
});
}
Run Code Online (Sandbox Code Playgroud)
我有 Sequelize 3.30.4 和 postgreSQL 9.6。
我希望 autoIncrement 为 true,因为我正在使用 postgreSQL uuid_generate_v4() 生成 UUID。
这里不是常规的续集用户,但让我指出,在 postgreql 中对非顺序列使用 autoIncrement 不是正确的方法。Postgresql 不提供默认的 uuid 数字生成器,但可以轻松添加扩展https://www.postgresql.org/docs/9.4/static/uuid-ossp.html。我相信你已经这样做了。
下一步是我们的sequelize.fn函数。
创建一个表示数据库函数的对象。这可以用在搜索查询中,在位置和顺序部分中,以及作为列定义中的默认值。
所以我们有
ladder_id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
default: sequelize.fn('uuid_generate_v4')
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是,autoIncrementPostgreSQL 被硬编码为使用 SERIAL 类型作为列,这与您选择的 UUID 相冲突。
尝试删除自动增量参数并使用默认值:
return sequelize.define('Ladder', {
ladder_id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: UUIDV4
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7034 次 |
| 最近记录: |