{ const result = await sequelize.query(-6ren">
gpt4 book ai didi

mysql - 使用 MYSQL : Raw query returns a "duplicate" result 进行 Sequelize

转载 作者:可可西里 更新时间:2023-11-01 07:50:52 25 4
gpt4 key购买 nike

我有这个执行原始查询的方法:

 Friendship.getFriends= async (userId)=>{

const result = await sequelize.query(`select id,email from users where
users.id in(SELECT friendId FROM friendships where friendships.userId =
${userId})`);

return result;
};

结果似乎包含完全相同的数据,但是两次:

[ [ TextRow { id: 6, email: 'example3@gmail.com' },
TextRow { id: 1, email: 'yoyo@gmail.com' } ],
[ TextRow { id: 6, email: 'example3@gmail.com' },
TextRow { id: 1, email: 'yoyo@gmail.com' } ] ]

这个查询实际上应该只找到两条记录(id 的 1 和 6),但它返回了一个包含相同记录两次的数组。

谁能给我解释一下这是怎么回事?

编辑:模型:

module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
email: { type: DataTypes.STRING, unique: true },
password: DataTypes.STRING,
isActive:{type:DataTypes.BOOLEAN,defaultValue:true}
});

module.exports = (sequelize, DataTypes) => {
const Friendship = sequelize.define('Friendship', {
userId: DataTypes.INTEGER,
friendId: DataTypes.INTEGER,
});

最佳答案

尝试在第二个参数中将查询类型设置为 SELECT。

sequelize.query(queryString, {type: sequelize.QueryTypes.SELECT})

关于mysql - 使用 MYSQL : Raw query returns a "duplicate" result 进行 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53460754/

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