我有sequelize的问题,当我想通过关联模型命令我的查询结果时,sequelize抛出这个错误:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:无法找到模型的有效关联,'productLanguage'
这些是我的文件:
** Context.js **
const Sequelize = require('sequelize');
const sequelize = new Sequelize("postgres://postgres:123456@localhost:5432/sampleDB");
module.exports = {
Sequelize: Sequelize,
sequelize: sequelize
}
user.js的
const context = require('../context');
module.exports = context.sequelize.define('user', {
name: context.Sequelize.STRING,
},{
freezeTableName: true
});
Product.js
const context = require('../context');
module.exports = context.sequelize.define('product', {
slug: context.Sequelize.STRING,
price: context.Sequelize.DECIMAL(10,2),
},{
freezeTableName: true
});
ProductLanguage.js
const context = require('../context');
module.exports = context.sequelize.define('productLanguage', {
name: context.Sequelize.STRING,
},{
freezeTableName: true,
timestamps: false
});
Language.js
const context = require('../context');
module.exports = context.sequelize.define('language', {
name: context.Sequelize.STRING,
slug: context.Sequelize.STRING,
},{
freezeTableName: true
});
db.js
var context = require('./context');
var User = require('./models/User'),
Product = require('./models/Product'),
ProductLanguage = require('./models/ProductLanguage'),
Language = require('./models/Language');
// ===================== ASSOCIATIONS =====================
// user 1:m Product
Product.belongsTo(User); // product owner
User.hasMany(Product);
// Product 1:m ProductLanguage m:1 Language
ProductLanguage.belongsTo(Product);
Product.hasMany(ProductLanguage);
ProductLanguage.belongsTo(Language);
Language.hasMany(ProductLanguage);
module.exports = {
Sequelize: context.Sequelize,
sequelize: context.sequelize,
models: {
Product: Product,
User: User,
ProductLanguage: ProductLanguage,
Language: Language
}
}
最后这是我的查询
app.get('/', async (req, res, next)=>{
var result = await db.models.User.findAll({
include:[
{
model: db.models.Product,
attributes: ['price'],
include: [
{
model: db.models.ProductLanguage,
attributes: ['name'],
include: [
{
model: db.models.Language,
attributes: ['name'],
}
]
}
]
}
],
order:[
[db.models.ProductLanguage, 'name', 'desc']
],
attributes: ['name']
});
res.send(result);
});
查询工作正常没有“订单”部分,所以我认为问题应该是这一个:
注意:我在youtube上搜索了stackoverflow并在4天内搜索了文档,但没有找到。
我使用这些依赖项:
"express": "^4.16.2",
"pg": "^6.4.2",
"pg-hstore": "^2.3.2",
"sequelize": "^4.32.2"
答案 0 :(得分:5)
我找到了解决方案。
我必须将所有关联的模型按顺序排列,因此正确的查询是:
order:[
[db.models.Product, db.sequelize.models.ProductLanguage, 'name', 'desc']
],
完整查询必须是:
var result = await db.models.User.findAll({
include:[
{
model: db.models.Product,
attributes: ['price'],
include: [
{
model: db.models.ProductLanguage,
attributes: ['name'],
include: [
{
model: db.models.Language,
attributes: ['name'],
}
]
}
]
}
],
order:[
[db.models.Product, db.sequelize.models.ProductLanguage, 'name', 'desc']
],
attributes: ['name']
});
我希望这会对其他人有所帮助。
答案 1 :(得分:1)
那些仍然无法得到结果的人,请尝试以下语法-
order:[[{ model: db.models.ProductLanguage, as: 'language_of_product' } , 'name', 'desc']]
答案 2 :(得分:-1)
除了Moradof的答案外,还需要注意的是,如果您为所包含的模型指定别名,那么还必须在order
语句中指定别名。
在前面的示例的基础上,我们得到:
var result = await db.models.User.findAll({
include:[
{
model: db.models.Product,
as: 'include1',
attributes: ['price'],
include: [
{
model: db.models.ProductLanguage,
as: 'include2',
attributes: ['name'],
include: [
{
model: db.models.Language,
attributes: ['name'],
}
]
}
]
}
],
order:[
[{ model: db.models.Product, as: 'include1' },
{ model: db.sequelize.models.ProductLanguage, as: 'include2' },
'name',
'desc']
],
attributes: ['name']
});
请注意,因为我在include语句中将Product
命名为include1
,所以在order语句中我也不得不将其命名为include1
。