gpt4 book ai didi

mysql - 序列化属于更改标识符

转载 作者:行者123 更新时间:2023-11-29 16:03:56 24 4
gpt4 key购买 nike

我正在尝试使用sequelize ORM 进行mysql 查询来选择friends.user_id 上的用户名。

到目前为止,我想出了这个解决方案,它工作得很好,但它选择 AS Users ON Friends.friend_id 而不是 AS Users ON Friends.user_id

这里描述了这些关系

db.Friends.belongsTo(db.Users, { as: 'UserOpUsers', sourceKey: 'friend_id', foreignKey: 'user_id' })

查询确实

SELECT `Friends`.`id` AS `friend_id`, `Users`.`username`, `Users`.`id`, `Friends`.`status` FROM `wd_friends_relations` AS `Friends` LEFT OUTER JOIN `info_users` AS `Users` ON `Friends`.`friend_id` = `Users`.`id` WHERE `Friends`.`friend_id` = 2;

但它应该使

SELECT `Friends`.`id` AS `friend_id`, `Users`.`username`, `Users`.`id`, `Friends`.`status` FROM `wd_friends_relations` AS `Friends` LEFT OUTER JOIN `info_users` AS `Users` ON `Friends`.`user_id` = `Users`.`id` WHERE `Friends`.`friend_id` = 2;

最佳答案

belongsTo关联中没有sourceKey http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-belongsTo ,它仅存在于 HasOne 和 HasMany 关系中。所以使用这样的东西,

db.Friends.belongsTo(db.Users, { as: 'UserOpUsers', foreignKey: 'user_id' })

它将创建这样的查询,

SELECT `Friends`.`id` AS `friend_id`, `Users`.`username`, `Users`.`id`, `Friends`.`status` FROM `wd_friends_relations` AS `Friends` LEFT OUTER JOIN `info_users` AS `Users` ON `Friends`.`user_id` = `Users`.`id` WHERE `Friends`.`friend_id` = 2;

关于mysql - 序列化属于更改标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916859/

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