【问题标题】:sequelize with postgres database not working after migration from mysql从 mysql 迁移后,postgres 数据库无法正常工作
【发布时间】:2015-05-04 20:39:15
【问题描述】:

我在 sequelize 中将 MySQL 数据库更改为 postgreSQL。但迁移后,我在表或模型中遇到大小写首字母问题...... 在我的 MySQL 版本正常工作之前,但在迁移之后,我收到了错误消息: 500 SequelizeDatabaseError: relation "Users" does not exist

我的用户模型:

module.exports = function(sequelize, Sequelize) {
  var User = sequelize.define("User", {
    // profile
    userlevel: Sequelize.STRING,
    restaurant: Sequelize.STRING,
    access: Sequelize.STRING,
    optionsid: Sequelize.STRING,
    email: Sequelize.STRING,
    name: Sequelize.STRING,
    gender: Sequelize.STRING,
    location: Sequelize.STRING,
    website: Sequelize.STRING,
    picture: Sequelize.STRING,
    // Oauth
    password: {
      type: Sequelize.STRING,
      set: function(v) {
        var salt = bcrypt.genSaltSync(5);
        var password = bcrypt.hashSync(v, salt);
        return this.setDataValue('password', password);
      }
    },
    .....

迁移文件:

"use strict";
module.exports = {
  up: function(migration, DataTypes, done) {
    migration.createTable("users", {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER
      },
      userlevel: {
        type: DataTypes.STRING,
        defaultValue: '5'
      },
      restaurant: {
        type: DataTypes.STRING,
        defaultValue: ''
      },
      access: {
        type: DataTypes.STRING,
        defaultValue: '1'
      },
      optionsid: {
        type: DataTypes.STRING,
        defaultValue: ''
      },
      email: {
        type: DataTypes.STRING,
        allowNull: false
      },
      name: {
        type: DataTypes.STRING,
        defaultValue: ''
      },
      gender: {
        type: DataTypes.STRING,
        defaultValue: ''
      },
      location: {
        type: DataTypes.STRING,
        defaultValue: ''
      },
      website: {
        type: DataTypes.STRING,
        defaultValue: ''
      },
      picture: {
        type: DataTypes.STRING,
        defaultValue: ''
      },
      password: {
        type: DataTypes.STRING
      },
      facebook: {
        type: DataTypes.STRING
      },
      twitter: {
        type: DataTypes.STRING
      },
      google: {
        type: DataTypes.STRING
      },
      tokens: {
        type: DataTypes.STRING
      },
      resetPasswordToken: {
        type: DataTypes.STRING
      },
      resetPasswordExpires: {
        type: DataTypes.DATE
      },
      createdAt: {
        allowNull: false,
        type: DataTypes.DATE
      },
      updatedAt: {
        allowNull: false,
        type: DataTypes.DATE
      }
    }).done(done);
  },
  down: function(migration, DataTypes, done) {
    migration.dropTable("users").done(done);
  }
};

如果我将 postgreSQL 中表的第一个字母更改为大写,一切正常...

【问题讨论】:

    标签: mysql postgresql sequelize.js postgresql-9.3


    【解决方案1】:

    PostgreSQL 将普通标识符的名称折叠为小写。所以usersUsersUSERS 都解析为标识符users

    分隔标识符不同。 (定界标识符用双引号括起来。)标识符"users""Users""USERS" 是三个不同的标识符。

    您的迁移创建了表"users"。 Sequelize 正在寻找表"Users"。 (分隔标识符——两个不同的表。)

    您可能应该将迁移中的标识符更改为“用户”。还有其他方法,但这是阻力最小的路径。如果这已经在生产中,您最好编写另一个将 "users" 重命名为 "Users" 的迁移。

    【讨论】:

    • 感谢您的回答!那么我的表格会有第一个大写字母吗?这是个好主意吗?
    • @Makromat 对 PostgreSQL 的通常建议是在单词之间使用带有下划线的小写标识符。但是,您使用的命名约定取决于您的 ORM 的期望。如果 Sequalize 没有约定,那么我建议您使用小写和下划线,这样您就不会一直引用您的标识符,但任何一致的都应该足够好。
    • PostgreSQL 不关心你是否使用分隔标识符。它们有点不方便,因为当您编写 SQL 时,您必须一直使用双引号。但是您使用的是 ORM,因此您不太可能编写太多 SQL。大多数(全部?)ORM 让你超越他们的约定。对于续集,请参阅Working with legacy tables
    • 该死的,这是续集的另一个坏部分(以及迁移和同步解耦)!所以你用“用户”迁移你的表,你没有选择在你的代码中称它为“用户”?有 3 个验证点(模型、迁移文件、自动同步的东西)所有这些都是误导性的,并且不提供一个健壮的模式。哦耶;如果“...(任何约定都没有提供理由)..”,则引用它,那会起作用
    • @MikeSherrill'CatRecall' 链接已损坏
    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多