On insert I get this error -> "All object keys mus...
# help
t
On insert I get this error -> "All object keys must match". I tried to set the autogenerated keys also but no luck. Any idea?
n
Hello @theuknowner! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
s
Can you provide example code of what you are trying please? otherwise its hard to assist you with the issue you are having.
n
theuknowner (2022-03-31)
t
@User Sure, I use the sample code that you have in Supabase-swift
do { let todo = Todo(task: "fix some issues in postgrest-swift", completed: true) let jsonData: Data = try JSONEncoder().encode(todo) database.from("todo").insert(values: jsonData).execute { (result) in switch result { case .success(let response): guard let data = response.body as? Data else { return } do { let todos = try JSONDecoder().decode([Todo].self, from: data) print(todos) } catch { print(error.localizedDescription) } case .failure(let error): print(error.localizedDescription) } } }catch { print(error.localizedDescription) }
I have this issue only in Swift. In JS - Kotlin there is no issue
g
Are you using supabase-swift or postgres-swift for your overall environment. The code example you have is from the postgres-swift only example which is different setup than the supabase-swift one as far as the example object structure...
t
I tried with both packages
Same result
If you check the packages which are bundled in supabase-swift, it has the postgres-swift in order to handle database
If i do this -> let jsonDict: [String: String] = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String : String] it works, but I don't have only String values in my Codable Item. I have booleans, ints etc..
and If I set it to [String:Any] I get -> "All object keys must match"
The example in Supabase-Swift should be replaced according to this issue -> https://github.com/supabase-community/supabase-swift/issues/4
s
@User can you share more of your code like where you instantiated the supabase client
t
struct Todo: Codable { var test: String var `id`: Int? var testNum: Int } let postgrClient = PostgrestClient( url: "xxxxxxxxxxx", headers: ["authorization": "Bearer xxxxxxxxxxxx" , "apikey": "xxxxxxxxx"], schema: "public") do { let todo = Todo(task: "fix some issues in postgrest-swift", testNum: 12) let jsonData: Data = try JSONEncoder().encode(todo) postgrClient.from("todo").insert(values: jsonData).execute { (result) in switch result { case .success(let response): guard let data = response.body as? Data else { return } do { let todos = try JSONDecoder().decode([Todo].self, from: data) print(todos) } catch { print(error.localizedDescription) } case .failure(let error): print(error.localizedDescription) } } }catch { print(error.localizedDescription) }
That's it. nothing special
g
You have test in your structure by use task in your todo set up
t
What I have tried so far: 1) If I set it to [String:String] it's ok BUT only if my Codable Item has String values
2) If I set to [String:Any] (that should be in order to support different types) I get -> "All object keys must match"
@User what do you mean?
g
let todo = Todo(task:
var test: S
t
Do you mean to assign it to a variable? I don't understand
g
struct Todo: Codable { var test: String var id: Int? var testNum: Int } So when your: let todo = Todo(task: "fix some issues in postgrest-swift", testNum: 12) Needs test: not task:
Assuming your table column is named test in the database
t
Yes task, I copy pasted wrong Struct
It's simple Gary, I have a table named todo
In this table I have 3 columns, 1) id autogenerated, 2) task Text, 3) testNum Int
that's all
autogenerated Int*
g
OK but your code you showed had test as the name in the structure. You might also have an uppercase problem with your table. Was it defined "testNum" with quotes. Just passing in column testNum may get coverted to lowercase. You really should avoid camelCase in postgres tables if possible. But I'm not sure you error is hitting that yet.
s
I'm not a Swift dev but from our example code you need to use a JSON dictionary, I'm not sure what JSONEncoder().encode does, but the example on the supabase-swift repo uses JSONSerialization.jsonObject along with the encoder https://github.com/supabase-community/supabase-swift#database
t
@User Yes I know about the camel case in postgres, but in JS - Kotlin I don;t have any issue with this
@User I need to use dictionary with type [String: Any] If I do this I get -> Protocol 'Any' as a type cannot conform to 'Encodable'
g
I am going to bow out as it could be swift specific and I've gone thru the easy stuff. You might ask in that repository, or in the swift section below. Unless @User has more ideas..
s
Ah found it I think
You need to use
postgrClient.database.from
instead of
postgrClient.from
t
I use the postgrest-swift package. So it's client.from
s
You should not be using that package directly
t
The Supabase-Swift needs the ".database.from" because the package is bundled
Why not?
I don't need Auth,Storage,Realtime (For Realtime I Use the Swift Phoenix Client)
g
@User In Swift thread, You just posted the same code with test in your object structure, but task in your todo setup. I thought you said the object structure was wrong...
t
I changed it, thanks
I found a workaround , It's totally wrong but it works
For some reason, I have to set all my fields to Strings, For example, testNum: String(12), if I have a bool value I have to make it like "true" etc..
I checked the db and It appends the values in Int,Text,Bool columns
but it's not working again if I have UUIDs .....