Node.js 未知列';User.ucode';在';字段列表';(本地主机正常,主机不正常)
很抱歉,这个问题看起来很愚蠢,但我的托管服务器出现了一个奇怪的问题,因为代码目前正在我自己的计算机上运行 我试图做的是从内容中查询字段Node.js 未知列';User.ucode';在';字段列表';(本地主机正常,主机不正常),node.js,sequelize.js,Node.js,Sequelize.js,很抱歉,这个问题看起来很愚蠢,但我的托管服务器出现了一个奇怪的问题,因为代码目前正在我自己的计算机上运行 我试图做的是从内容中查询字段title、id和category,从用户中查询用户的ucode,查询到每个结果的以下结构中: { title: "Some title", id: "123456", category: "11", ucode: "123ABC" } …然后根据创建日期对其进行降序排序。我是这样查询的,它已经在我的计算机上运行,并返回了我想要的
title
、id
和category
,从用户中查询用户的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
这里有人有什么指点吗?谢谢 由于用户是一个独立的
模型
,表名可能是小写的用户
您应该包括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(...)
不知怎的。。。我刚从
User.ucode
改为User.ucode
,它神奇地工作了。因此,完整查询现在变为(也要感谢@Sravan):
我仍然不确定为什么大写字母在我的计算机上有效,但在主机上无效。但这可能是因为在我的主机上,表被初始化为
user
,而不是user
,并且/或者区分大小写。或者可能是因为我使用的是MySQL 5.7,而主机使用的是5.5。你是否在live中迁移了user
表?我已经完全重置了整个数据库,但仍然没有运气。检查你的数据库,并检查user
字段是否由“field”显示?你是指我的数据库列吗?如果是这样,User
就是一个模型。和ucode
作为User
表的一部分存在,并且(应该)可以通过指定User.ucode
进行查询。它在Content
asUser
中并不明确存在,但这确实稍微改变了结构,因为现在ucode
返回为{“User.ucode”:..}
您能发送完整的响应吗
谢谢您的帮助,但我只是通过将User.ucode
更改为User.ucode
解决了这个问题。我将把它放到我自己的回答中,所以,您的表名可能是user
,而不是您发现的UserGoo。我已经用同样的方法更新了答案,以便对任何人都有用
Content.findAll({
attributes: ["title", "id", "category", "user.ucode"],
include: [{
model: User,
attributes: [],
required: true
}],
order: [['createdAt', 'DESC']],
offset: offset,
limit: 50,
raw: true
}).then(...)