使用外键时的 Supabase Swift 解码问题

问题描述 投票:0回答:1

当提供指向外部表的

.select
修饰符时,值无法解码。我想知道这是否是我声明
select
查询的方式中的一个问题,是否与 postgres 有关或 Supabase 中的错误有关。

据我所知,

value
似乎正在解码错误的 JSON 字符串。如果我查看实际的网络响应,
account
值是一个对象,但是解码器认为它是一个字符串(大概是postgres中的外键)。在左侧您可以看到原始 API 响应,在右侧您可以看到 Supabase 正在尝试解码的内容。

重现

型号

struct Transaction: Codable {
    var id: UUID
    var name: String
    var subtitle: String?
    var merchant: String?
    var amount: Decimal
    var tax_deductible: Bool = false
    var date: Date
    var account: TransactionAccount?
}

enum AccountType: String, Codable, CaseIterable {
    case debit
    case savings
    case loan
    case credit
    case cash
}

struct TransactionAccount: Codable, Hashable {
    var id: UUID
    var name: String
    var description: String?
    var type: AccountType
    var color: String
}

SDK使用

do {
            return try await Database.shared
                .client
                .database
                .from("transactions")
                .select("*, account(*)")
                .in("type", value: scope.transactionTypes.map({ $0.rawValue }))
                .execute()
                .value
        } catch {
            return []
        }

快速错误

Printing description of error:
▿ DecodingError
  ▿ typeMismatch : 2 elements
    - .0 : Swift.Dictionary<Swift.String, Any>
    ▿ .1 : Context
      ▿ codingPath : 2 elements
        ▿ 0 : _JSONKey(stringValue: "Index 2", intValue: 2)
          ▿ rep : Rep
            - index : 2
        - 1 : CodingKeys(stringValue: "account", intValue: nil)
      - debugDescription : "Expected to decode Dictionary<String, Any> but found a string instead."
      - underlyingError : nil

预期行为

文档清楚地表明我应该能够执行此操作并从外部表中获取完整的对象。 https://supabase.com/docs/reference/swift/select?example=querying-foreign-table-with-count

其他背景

如果我将每个值单独添加到查询中,请求将按预期工作:

do {
            return try await Database.shared
                .client
                .database
                .from("transactions")
                .select(
                """
            id,
            description,
            amount,
            type,
            date,
            name,
            tax_deductible,
            account(*)
            """
            )
                .in("type", value: scope.transactionTypes.map({ $0.rawValue }))
                .execute()
                .value
        } catch {
            return []
        }
    }
ios swift xcode supabase
1个回答
0
投票

这已在 GH 问题上得到解答(https://github.com/supabase/supabase-swift/issues/322)。

© www.soinside.com 2019 - 2024. All rights reserved.