Steam中的PivotProtocol

时间:2017-07-03 13:47:29

标签: vapor

如何使用相同的对象制作Pivot? 我试过这个但是得到了一个mySQL错误:

final class UserParent: Model, PivotProtocol {
    var storage = Storage()

    typealias Left = User
    typealias Right = User

    static var leftIdKey: String = "user_id_parent"
    static var rightIdKey: String = "user_id_child"

    init(row: Row) throws { }

    func makeRow() throws -> Row {
        let row = Row()
        return row
    }

}

//MARK: - Preparation
extension UserParent: Preparation {
    static func prepare(_ database: Database) throws {
        try database.create(self, closure: { builder in
            builder.id()
            builder.int("user_id_parent")
            builder.int("user_id_child")
        })
    }

    static func revert(_ database: Database) throws {
        try database.delete(self)
    }
}

然后我在我的用户对象上有这个

var users: Siblings<User, User, UserParent> {
    return siblings()
}

但是我得到了mySQL错误

[MySQL Error: Unknown column 'user_parents.user_id' in 'where clause']

1 个答案:

答案 0 :(得分:0)

完成tanner0101的回答:

您的模型是正确的,但您的Siblings实现也必须指定自定义键。

var parentUsers: Siblings<User, User, UserParent> {
  return siblings(localIdKey: "user_id_child", foreignIdKey: "user_id_parent")
}
var childUsers: Siblings<User, User, UserParent> {
  return siblings(localIdKey: "user_id_parent", foreignIdKey: "user_id_child")
}