我很抱歉这个看起来很愚蠢的问题,但我的托管服务器遇到了一个奇怪的问题,因为代码目前正在我自己的计算机上运行。
我想要做的是将内容中的 title
、 id
和 category
字段以及 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/