node.js - 'User.ucode' 中的未知列 'field list'(本地主机正常,主机不正常)

标签 node.js sequelize.js

我很抱歉这个看起来很愚蠢的问题,但我的托管服务器遇到了一个奇怪的问题,因为代码目前正在我自己的计算机上运行。

我想要做的是将内容中的 titleidcategory 字段以及 User 中的用户 ucode 查询到每个结果的以下结构中:

{
    title: "Some title",
    id: "123456",
    category: "11",
    ucode: "123ABC"
}

...然后根据创建日期对它进行降序排序。这是我的查询方式,它已经在我的计算机上运行并返回了我想要的结构:
//Relations
User.hasMany(Content, { foreignKey: 'userID' }); //userID is the PK of Users table
Content.belongsTo(User, { foreignKey: 'userID' });

//Query
Content.findAll({
    attributes: ["title", "id", "category", "User.ucode"],
    include: [{
        model: User,
        attributes: [],
        required: true
    }],
    order: [['createdAt', 'DESC']],
    offset: offset,
    limit: 50,
    raw: true
}).then(...)

我在我的主机上遇到错误,但在本地主机上没有:
SequelizeDatabaseError: Unknown column 'User.ucode' in 'field list'\n
at Query.formatError ............

我不知道这是否可能是由版本差异引起的,尽管很小,但是我的计算机运行的是 v8.9.4,而我的主机运行的是 v8.9.3。

这里有人有一些指示吗?谢谢!

最佳答案

由于用户是 separate model ,表名可能是小写的 user
您应该包括 user.ucode

Content.findAll({
    attributes: ["title", "id", "category", "user.ucode"],
    include: [{
        model: Model2,
        attributes: [],
        required: true
    }],
    order: [['createdAt', 'DESC']],
    offset: offset,
    limit: 50,
    raw: true
}).then(...)

关于node.js - 'User.ucode' 中的未知列 'field list'(本地主机正常,主机不正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49917991/

相关文章:

javascript - Sinon.js stub 对象内部的匿名函数

node.js - 如何在 .select 语句中使用带有 knex 的 pgcrypto? (Postgres 数据库)

node.js - 通过 Sequelize 连接表嵌套关联数据

javascript - Sequelize 查找软删除的行

mysql - 即将发生的事件的日期更改的 SQL 查询

node.js - 使用 Axios 在 Sequelize 上更新请求 (PUT) 超时

javascript - WebRTC 数据通道服务器到客户端的 UDP 通信

javascript - 导入到 Node.js TypeScript 文件中的包无法被命名空间部分声明识别?

typescript - 使用 Typescript 进行 Sequelize 无法正确解析类型

node.js - mocha,用于 nodejs 中的 post 的 chai 测试