Vapor 中相同模型之间的兄弟关系 [英] Siblings relationship between same models in Vapor

查看:47
本文介绍了Vapor 中相同模型之间的兄弟关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 User 模型,我想向其中添加一个 friends 属性.朋友,应该是其他User.

I have a User model which I want to add a friends property to. Friends, are supposed to be other Users.

我创建了UserFriendsPivot:

final class UserFriendsPivot: MySQLPivot, ModifiablePivot {
    var id: Int?
    var userID: User.ID
    var friendID: User.ID

    typealias Left = User
    typealias Right = User

    static var leftIDKey: WritableKeyPath<UserFriendsPivot, Int> {
        return \.userID
    }

    static var rightIDKey: WritableKeyPath<UserFriendsPivot, Int> {
        return \.friendID
    }

    init(_ user: User, _ friend: User) throws {
        self.userID   = try user  .requireID()
        self.friendID = try friend.requireID()
    }
}

extension UserFriendsPivot: Migration {
    public static var entity: String {
        return "user_friends"
    }
}

我给 User 添加了 friends 属性:

I added the friends property to User:

var friends: Siblings<User, User, UserFriendsPivot> {
    return siblings()
}

现在,我在 return Brothers() 行上看到以下错误:

Now, I'm seeing the following error on the line with return siblings():

'siblings(related:through:)'的歧义使用

Ambiguous use of 'siblings(related:through:)'

我尝试将其替换为:

return siblings(related: User.self, through: UserFriendsPivot.self)

...没有任何运气.

我知道这两个代码片段应该工作,因为我直接从我在 EventUser 之间建立的另一个兄弟关系复制了它们> 效果很好.
我看到的唯一区别是我正在尝试在相同模型之间建立关系.

I know that the two code snippets should work, because I straight-up copied them from another siblings relationship I built between Event and User that is working just fine.
The only difference I'm seeing is that I'm trying to build a relationship between the same models.

我能做什么?

推荐答案

这里的问题是在相同-Model (User-User) 兄弟关系中,Fluent无法推断您指的是哪个兄弟姐妹 - 需要指定双方.

The issue here is that in a same-Model (User-User) siblings relation, Fluent cannot infer which sibling you are referring to – the sides need to be specified.

extension User {
    // friends of this user
    var friends: Siblings<User, User, UserFriendsPivot> {
        siblings(UserFriendsPivot.leftIDKey, UserFriendsPivot.rightIDKey)
    }

    // users who are friends with this user
    var friendOf: Siblings<User, User, UserFriendsPivot> {
        siblings(UserFriendsPivot.rightIDKey, UserFriendsPivot.leftIDKey)
    }
}

另一个相同的-Model 后果是您将无法使用 attach 便捷方法添加到数据透视表中,而需要手动创建:

The other same-Model consequence is that you will not be able to use the attach convenience method to add to the pivot table, and need to manually create instead:

let pivot = try UserFriendsPivot(user, friend)
pivot.save(on: req)

(还有其他方法可以解决这个问题,我只是发现上面这些简单的方法最容易使用.指定边和反转关键位置以获得逆关系是重要的概念.)

(There are other approaches to work around this, I just find these straightforward ways above the easiest to use. Specifying the sides and reversing the key positions to obtain the inverse relation are the important concepts.)

grundoon 回答

这篇关于Vapor 中相同模型之间的兄弟关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆