【问题标题】:Siblings relationship between same models in VaporVapor中相同模型之间的兄弟关系
【发布时间】:2019-03-16 20:41:42
【问题描述】:

我有一个User 模型,我想向它添加一个friends 属性。朋友,应该是其他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"
    }
}

我将friends 属性添加到User

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

现在,我在return siblings() 一行看到以下错误:

'siblings(related:through:)' 的模糊使用

我尝试将其替换为:

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

...运气不好。

我知道这两个代码 sn-ps 应该工作,因为我直接从我在 EventUser 之间建立的另一个兄弟姐妹关系中复制了它们,这工作得很好。 我看到的唯一区别是我正在尝试在相同模型之间建立关系。

我能做什么?

【问题讨论】:

    标签: swift relationship vapor


    【解决方案1】:

    尝试将您的 friends 定义替换为:

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

    编辑:

    它应该与LeftRight 作为同一个表一起使用,但似乎失败了,因为别名解析为基值。 IE。 Xcode 中的自动完成功能显示所有 siblings 的候选者都最终属于类型:

    Siblings<User, User, UserFriendsPivot> siblings(...)
    

    代替:

    Siblings<User, UserFriendsPivot.Right, UserFriendsPivot> siblings(...)
    

    和类似的。

    我建议在 GitHub 上提出一个错误。同时,如何创建一个具有不同名称和设置的User 副本:

    static let entity = "User"
    

    使用相同的物理表。不漂亮,但它可能会让你工作。

    【讨论】:

    • 那么siblings { get }User中的实现应该是什么?此外,Siblings&lt;User, Right, ...&gt; 会抛出错误Use of undeclared type 'Right'
    • 移动 Right 的 typealias 使其成为全局范围。
    • 哦,但是 Right 将永远是 User... 我会改为 UserFriendsPivot.Right。那么siblings'的实现呢?
    • 您是否尝试过(假设兄弟姐妹呼叫在用户中)尝试兄弟姐妹(相关:UserFriendsPivot.Right,通过:UserFriendsPivot)?
    • 我计划对我的一个应用程序进行更改,这也需要它来工作!祝你好运。
    【解决方案2】:

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

    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后果是您将无法使用附加便捷方法添加到数据透视表中,而需要手动创建:

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

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


    grundoon回答

    【讨论】:

      猜你喜欢
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2018-07-11
      相关资源
      最近更新 更多