gpt4 book ai didi

javascript - 尝试在 Sequelize 中排序但出现错误 "Unable to find a valid association for model"

转载 作者:行者123 更新时间:2023-12-04 17:14:32 26 4
gpt4 key购买 nike

我正在尝试在 Sequelize 中订购包含关联(多对多)的 findByPk 结果,但我遇到了一些问题。我不断收到错误:

Error: Unable to find a valid association for model, 'Item'


查询:
        Guest.findByPk(id,
{
include: { model: db.Item, as: 'items' },
order: [
[ { model: db.sequelize.models.Item, as: 'items' }, 'dexId', 'ASC' ]
]
})
.then(data => {
res.status(200).json({ items: data.items});
})
.catch(err => {
const error = new createError(500, "Error retrieving Guest with id=" + id);
return next(error);
});
商品型号:
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Item extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
Item.belongsToMany(models.Guest, {
//through: 'GuestItems',
through: {
model: 'GuestItems',
unique: false
},
constraints: false,
as: 'guests',
foreignKey: 'itemId',
otherKey: 'guestId'
});
Item.belongsToMany(models.User, {
//through: 'UserItems',
through: {
model: 'UserItems',
unique: false
},
constraints: false,
as: 'users',
foreignKey: 'itemId',
otherKey: 'userId'
});
}
}

Item.init({
dexId: {
allowNull: true,
type: DataTypes.INTEGER,
defaultValue: null
},
name: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
description: DataTypes.STRING,
filename: DataTypes.STRING,
}, {
sequelize,
modelName: 'Item',
paranoid: true
});
return Item;
};
嘉宾模特
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Guest extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
Guest.belongsToMany(models.Item, {
through: 'GuestItems',
as: 'items',
foreignKey: 'guestId',
otherKey: 'itemId'
});
}
}
Guest.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
token: DataTypes.STRING,
role: {
type: DataTypes.ENUM,
values: ["guest"],
defaultValue: "guest"
},
lastIPAddress: DataTypes.STRING
}, {
sequelize,
modelName: 'Guest',
associations: true
});
return Guest;
};

我知道 db.sequelize.models.Item 存在的事实,我也试过调用 db.Item (它再次存在并在其他地方使用) - 两者都不起作用。
我也试过
  [ db.sequelize.models.Item, 'dexId', 'ASC' ]
而不是 { as 'items' } 位,但我仍然收到该错误。
我正在使用最新版本的 Sequelize 和 Postgresql

最佳答案

Guest.findByPk(id, {
include: {model: db.Item, as: 'items', required: true},
order: [[sequelize.literal('"items"."dexId"'), 'ASC']]
})
.then((data) => {
res.status(200).json({items: data.items});
})
.catch((err) => {
return next(error);
});

关于javascript - 尝试在 Sequelize 中排序但出现错误 "Unable to find a valid association for model",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68925794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com