swift - 在 Vapor 中使用 Fluent 时连接崩溃

标签 swift postgresql join vapor vapor-fluent

我有两个模型,它们具有一对多关系。这是我的类(class)。

预订

final class Booking: PostgreSQLModel {
    /// The unique identifier for this `Todo`.
    var id: Int?

    /// A title describing what this `Todo` entails.
    var user_id: User.ID
    var product_id: Product.ID
    var count: Int

    /// Creates a new `Todo`.
    init(id: Int? = nil, user_id: User.ID, product_id: Product.ID, count: Int) {
        self.id = id
        self.user_id = user_id
        self.product_id = product_id
        self.count = count
    }
}
extension Booking {
    var user: Parent<Booking, User> {
        return parent(\.user_id)
    }
    var product: Parent<Booking, Product> {
        return parent(\.product_id)
    }
}

产品

final class Product: PostgreSQLModel {
    /// The unique identifier for this `Todo`.
    var id: Int?

    /// A title describing what this `Todo` entails.
    var name: String
    var image_url: String
    var description: String
    var price: Int?

    /// Creates a new `Todo`.
    init(id: Int? = nil, name: String, image_url: String, description: String, price: Int) {
        self.id = id
        self.name = name
        self.image_url = image_url
        self.description = description
        self.price = price
    }
}
extension Product {
    var bookings: Children<Product, Booking> {
        return children(\.product_id)
    }
}

现在我想获取用户的所有预订,并且每次预订我也想获取产品信息。所以为此我尝试加入BookingProduct表但这会引发异常。

Fatal error: 'try!' expression unexpectedly raised an error: ⚠️ CoreError: Parent<Booking, Product> does not conform to ReflectionDecodable. - id: CoreError.ReflectionDecodable : file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang_Fall2018/swiftlang_Fall2018-1000.11.42/src/swift/stdlib/public/core/ErrorType.swift, line 184 Program ended with exit code: 9

这是我的加入代码。

let booking = Booking.query(on: req).join(\Product.bookings, to:\Booking.product).filter(\.user_id == userID).decode(BookingM.self).all()

最佳答案

首先,对于您所拥有的查询,您不需要加入 Product 表,因为您从不对其进行查询或解码。

您的加入声明:

.join(\Product.bookings, to:\Booking.product)

不正确。您不应加入 Product.bookings 属性,而应加入 Product.id 属性,因为产品的 ID 就是 Booking.product 的 ID。属性包含。

所以你的 Fluent 查询应该是这样的:

let booking = Booking.query(on: req).join(\Booking.product, to:\Product.id).filter(\.user_id == userID).all()

我删除了 .decoding 调用,因为查询也已经对查询结果进行了解码 Booking

要使用您的 bookings 属性,您的查询将如下所示:

let booking = product.bookings.query(on: req).filter(\Booking.user_id == userID).all()

关于swift - 在 Vapor 中使用 Fluent 时连接崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55899009/

相关文章:

swift - 如何使用 RxSwift 获取分页页面的所有页面 url

ios - 从数组中删除匹配条目

ios - 如何在与默认目标中的数据不同的数据/应用程序(SQLite db、UserDefaults)上运行 Xcode 测试?

postgresql - 在 PostgreSQL 中搜索字符串中的值

php - 拉拉维尔 : to join 2 join OR order with null

mysql - 将表连接成 4 列

ios - 仅纵向应用程序中的 AVPlayerViewController 全屏旋转行为

django - 提高 Django Slice 步骤的性能

postgresql - 为什么 PostgreSQL 抛出 "FULL JOIN is only supported with merge-joinable or hash-joinable join conditions"

mysql - 简单左连接的记录加倍问题