sequelize.js - Sequelize:如果声明不是基于 PK 并且适用于 PK 搜索,getOne() 将抛出“字段列表中的未知列 (...)”
问题描述
我正在使用getOne()
函数来提取具有关联模型的用户数据。如果我PK
在这样的where
子句中使用:where: { ID: 128 }
- 它工作正常。这意味着关联和查询配置是正确的。
但是,如果我试图按email
这样的字段查找用户:where: { email: 'email_of@user_with_ID_128.com' }
- Sequelize 会为最深关联模型的字段引发错误Unknown column (...) in field list
。电子邮件是正确的,它指向与按 ID 搜索相同的用户。如果我从查询配置或设置中排除关联required: false
- 查询成功并且可以使用email
.
这是查询行:
return models.User.findOne(config).then(user => { console.log(user) });
和选项/配置对象:
config = {
where: { ID: 128 }, // THIS WORKS
// OR
where: { email: 'email_of@user_with_ID_128.com' }, // THIS THROWS ERROR
include: [
{
nested: true,
model: models.UserComponent,
attributes: ['ID', 'isDisplayed', 'displayIndex'],
include: [
{
model: models.Component,
required: true,
include: [
{
model: models.ComponentType, // error relates to this model
required: true // doesn't throw error if set to false
},
{
model: models.ObjectType, // and error relates to this model
required: true // doesn't throw error if set to false
},
{
model: models.ComponentContent,
required: false
}
]
}
]
}
],
nested: true
};
型号定义:
const User = connectionPool.define('user', {
ID: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
email: {
type: Sequelize.STRING,
allowNull: false
},
surname: {
type: Sequelize.STRING,
allowNull: false,
}
});
const Component = connectionPool.define('component', {
ID: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
defaultIsDisplayed: {
type: Sequelize.BOOLEAN,
allowNull: false
},
defaultDisplayIndex: {
type: Sequelize.INTEGER,
allowNull: false
},
obligatory: {
type: Sequelize.BOOLEAN,
allowNull: false,
default: false
}
}, {
defaultScope: {
attributes: {
exclude: ['createdAt', 'updatedAt',
'componentContentID', 'objectTypeID',
'componentTypeID']
}
}
}
);
const UserComponent = connectionPool.define('user_component', {
ID: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
isDisplayed: {
type: Sequelize.BOOLEAN,
allowNull: false
},
displayIndex: {
type: Sequelize.INTEGER,
allowNull: false
}
}, {
defaultScope: {
attributes: { exclude: ['createdAt', 'updatedAt', 'componentID', 'userID', 'ID'] }
}
});
const ComponentType = connectionPool.define('component_type', {
ID: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
name: {
type: Sequelize.STRING,
allowNull: false
}
}, {
defaultScope: {
attributes: { exclude: ['ID', 'createdAt', 'updatedAt'] }
}
});
const ComponentContent = connectionPool.define('component_content', {
ID: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
content: {
type: Sequelize.STRING,
allowNull: false
}
});
协会:
models.Component.belongsTo(models.ComponentContent, { foreignKey: 'componentContentID', onDelete: 'CASCADE' });
models.Component.belongsTo(models.ComponentType, { foreignKey: 'componentTypeID', onDelete: 'CASCADE' });
models.Component.belongsTo(models.ObjectType, { foreignKey: 'objectTypeID', onDelete: 'CASCADE' });
models.Component.hasMany(models.UserComponent, { foreignKey: 'componentID', onDelete: 'CASCADE' });
models.UserComponent.belongsTo(models.Component, { foreignKey: 'componentID', onDelete: 'CASCADE' });
models.User.hasMany(models.UserComponent, { foreignKey: 'userID', onDelete: 'CASCADE' });
where
如果语句基于PK
并且未能将其他字段作为条件,我认为查询将正确附加关联没有逻辑上的原因。文档没有给出任何提示。有谁知道这种行为可能是什么原因?我想避免预先user
查找ID
或不需要相关模型。
解决方案
推荐阅读
- php - 在moodle中添加新的等级类型
- reactjs - 在状态数组中反应更改数组
- vue.js - 在 vuetify 迷你图中有不同的条形颜色
- css - 反应:在悬停时更改 css 属性
- oauth-2.0 - 转换 kerberos 票证以获取 Azure AD 访问令牌
- javascript - 如何在 dncalendar 中动态插入日期?
- react-native - 反应本机参考错误找不到变量:样式
- wordpress - WordPress:如何停用免费版。插件,在Pro之前会被激活吗?
- jquery - Angular 项目中的 Fomantic-UI:“.popup 不是函数”
- python - Python 3 谷歌驱动 API | 更新文件错误