在 Vapor 4 中,我创建了一个用户,它具有这样的“齿轮”:
final class User: Model, Content {
init() {}
static var schema: String = "user"
@ID(key: .id)
var id: UUID?
@Field(key: "name")
var name: String?
@Children(for: \.$user)
var gear: [Gear]
}
final class Gear: Model, Content {
init() {}
static var schema: String = "gear"
@ID(key: .id)
var id: UUID?
@Parent(key: "userId")
var user: User
}
然后,当我得到这些数据时,我会:
func index(req: Request) throws -> EventLoopFuture<[User]> {
return User.query(on: req.db).with(\.$gear).all()
}
比较流畅和简单。除了ID被强制为UUID,而数据库中的表只能采用“文本”类型,所以我收到错误:
{
"error": true,
"reason": "server: operator does not exist: text = uuid (op_error)"
}
所以有人知道如何解决这个问题吗?
我想要的响应结构类似于:
{
"id": "w32423tsdg3w",
"name": "username",
"gear": [{
"id": "1"
},
{
"id": "2"
}
]
}