当提供指向外部表的
.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 []
}
}
这已在 GH 问题上得到解答(https://github.com/supabase/supabase-swift/issues/322)。