【问题标题】:Sequelize migration fails (postgres)Sequelize 迁移失败(postgres)
【发布时间】:2023-03-14 17:51:01
【问题描述】:

我正在尝试使用 sequelize 将 postgresql 与我的节点应用程序一起使用。但我不能让它工作。当我运行sequelize -m 时,我得到这个输出:

Loaded configuration file "config/config.json".
Using environment "development".
Running migrations...
20130916100313-create-table-usuarios.js
Completed in 21ms

events.js:74
        throw TypeError('Uncaught, unspecified "error" event.');
              ^
TypeError: Uncaught, unspecified "error" event.
    at TypeError (<anonymous>)
    at EventEmitter.emit (events.js:74:15)
    at null.<anonymous> (/home/alejo/workspace/cloudlogger/api/node_modules/sequelize/lib/migrator.js:95:44)
    at EventEmitter.emit (events.js:98:17)
    at module.exports.finish (/home/alejo/workspace/cloudlogger/api/node_modules/sequelize/lib/query-chainer.js:138:30)
    at exec (/home/alejo/workspace/cloudlogger/api/node_modules/sequelize/lib/query-chainer.js:92:16)
    at onError (/home/alejo/workspace/cloudlogger/api/node_modules/sequelize/lib/query-chainer.js:72:11)
    at EventEmitter.emit (events.js:95:17)
    at /home/alejo/workspace/cloudlogger/api/node_modules/sequelize/lib/migration.js:65:19
    at null.<anonymous> (/home/alejo/workspace/cloudlogger/api/node_modules/sequelize/lib/emitters/custom-event-emitter.js:52:38)

这是我的 config.json:

{
  "development": {
    "username": "cloudlogger",
    "password": "foobar",
    "database": "cloudlogger_dev",
    "dialect":"postgres",
    "protocol":"postgres",
    "host": "127.0.0.1"
  },
  "test": {
    "username": "cloudlogger",
    "password": "foobar",
    "database": "cloudlogger_test",
    "dialect":"postgres",
    "protocol":"postgres",    
    "host": "127.0.0.1"
  },
  "production": {
    "username": "cloudlogger",
    "password": "foobar",
    "database": "cloudlogger_pro",
    "dialect":"postgres",
    "protocol":"postgres",
    "host": "127.0.0.1"
  }
}

这是 20130916100313-create-table-usuarios.js

module.exports = {
  up: function(migration, DataTypes, done) {
    migration.createTable('Usuario',{
      nombre: {
        type: DataTypes.STRING,
        allowBlank: false,
      },
      username: {
        type: DataTypes.STRING,
        unique: true,
      },
      genero: {
        type:   DataTypes.ENUM,
        values: ['Hombre', 'Mujer']
      },
      email: {
        type: DataTypes.STRING,
        unique: true,
        allowBlank: false,
      },
      password_digest: {
        type: DataTypes.STRING,
        allowBlank: false,
      },
      remember_token: DataTypes.STRING,
      superadministrador: {
        type: DataTypes.BOOLEAN,
        defaultValue: false
      },
      token: DataTypes.STRING,
      fecha_token: {
        type: DataTypes.DATE,
        defaultValue: new Date(0)
      },
      lastLogin: DataTypes.DATE
    }).complete(done);
  },
  down: function(migration, DataTypes, done) {
    migration.dropAllTables().complete(done);
  }
}

编辑

如果我评论或更改此行,我隔离了错误:

      genero: {
        type:   DataTypes.ENUM,
        values: ['Hombre', 'Mujer']
      },

效果很好。好像是ENUM类型的问题

【问题讨论】:

    标签: javascript node.js orm database-migration sequelize.js


    【解决方案1】:

    我也有同样的问题。查看 Postgres 源代码,似乎 Enum 必须在分配给列类型之前单独创建。

    我不确定 sequelize 是否支持这个操作

    来源:http://www.postgresql.org/docs/9.2/static/datatype-enum.html

    【讨论】:

      【解决方案2】:

      您的 ENUM 语法不正确。应该是这样的:

      type: DataTypes.ENUM('Hombre', 'Mujer')
      

      如果您有更多问题,请查看data types 上的续集文档

      【讨论】:

      猜你喜欢
      • 2016-01-01
      • 2012-07-21
      • 2017-06-26
      • 2016-07-19
      • 1970-01-01
      • 2019-05-04
      • 2021-01-23
      • 1970-01-01
      • 2013-08-15
      相关资源
      最近更新 更多