【问题标题】:Underscore gets removed下划线被删除
【发布时间】:2017-03-23 19:51:46
【问题描述】:

Sequelize 删除了我的外键中的下划线。

Role.belongsToMany(User, { foreignKey: 'user_id', through: UserRole });

结果如下:

Unknown column 'UserRole.UserId' in 'field list'

因为该列名为 user_id,而不是 UserId。

即使设置了underscore: true 选项,它也会这样做。

有没有办法解决这个问题,这让我发疯了,因为,我不知道我做错了还是续集。

UserRole.js:

module.exports = {
  attributes: {
    roleId: {
      type: Sequelize.INTEGER(11),
      field: 'role_id',
      primaryKey: true
    },
    userId: {
      type: Sequelize.INTEGER(11),
      field: 'user_id',
      primaryKey: true
    }
  },
  associations: function() {
      UserRole.belongsTo(User, { foreignKey: 'user_id' });
      UserRole.hasOne(Role, { foreignKey: 'id' });
  },
  options: {
    tableName: 'user_roles',
    timestamps: false,
    classMethods: {},
    instanceMethods: {},
    hooks: {}
  }
}

角色.js

module.exports = {
  attributes: {
    id: {
      type: Sequelize.INTEGER(11),
      primaryKey: true
    },
    name: {
      type: Sequelize.STRING(50),
    },
    description: {
      type: Sequelize.STRING(50),
    },
  },
  associations: function() {
    Role.belongsToMany(User, { foreignKey: 'user_id', through: UserRole });
  },
  options: {
    tableName: 'roles',
    timestamps: false,
    classMethods: {},
    instanceMethods: {},
    hooks: {}
  }
}

【问题讨论】:

  • 您确定您已尝试开启underscore 选项吗?我在这里的代码中看不到这一点,这很重要。
  • @tadman 我应该睡觉了,忘记在 User 模型中添加 underscored 选项。非常感谢!
  • 发生在我们最好的人身上。很高兴你得到它!

标签: mysql node.js sequelize.js


【解决方案1】:

您似乎没有在模型中启用underscore 选项。如果设置 Sequelize 尊重您的命名约定偏好,并将按预期链接事物。

我更喜欢带下划线的名称,长期的 Rails 开发人员,以及混合大小写的列名称,结果似乎被破坏了。

【讨论】:

    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 2019-11-25
    • 2011-04-26
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多