supabase / postgrest-dart

Dart client for PostgREST
https://supabase.com
MIT License
136 stars 38 forks source link

Select contains jsonb array #51

Closed Aleclock closed 2 years ago

Aleclock commented 2 years ago

Bug report

Describe the bug

I'm not sure if this is a bug or if I am missing something. I have a jsonb array column containing object (Player) converted in json. I'm trying to fetch all rows whose jsonb array column contains a specific object.

The insert method works fine doing

await dbClient.from('polls')
  .insert([{
    ...
    "items" : poll.items.map((i) => i.toJson()).toList(),
    ...
  ])
  .execute()

and getting something like

[
  {
    "votes": 0,
    "player": {
      "id": "409",
      "name": "INSIGNE",
      "team": "Napoli"
    },
    "description": ""
  }
]

The select method gives me different errors and I can't fetch no rows.

To Reproduce

// Method 1
  .contains('items', poll.items.map((i) => i.toJson()).toList())

I get following error:

[GETX] Error: DbRequest Instance of 'DbRequest' invalid input syntax for type json
// Method 2
  .contains('items', jsonEncode(poll.items))

I get following error:

[GETX] "Error: DbRequest Instance of 'DbRequest' malformed array literal: "[{"player":{"id":"409","name":"INSIGNE","team":"Napoli"},"votes":0,"description":""},{"player":{"id":"2530","name":"IBRAHIMOVIC","team":"Milan"},"votes":0,"description":""}]""

Expected behavior

Given a list of objects, I want to select from table all rows that partially match (contains at least one element) with the element stored in jsonb array column.

Thank you in advance.

bdlukaa commented 2 years ago

Maybe .contains('items', json.encode(poll.items.map((i) => i.toJson()).toList())) ?

Aleclock commented 2 years ago

Maybe .contains('items', json.encode(poll.items.map((i) => i.toJson()).toList())) ?

unfortunately it doesn't work, I get same error

"Error: DbRequest Instance of 'DbRequest' malformed array literal: "[{"player":{"id":"409","name":"INSIGNE","team":"Napoli"},"votes":0,"description":""},{"player":{"id":"2530","name":"IBRAHIMOVIC","team":"Milan"},"votes":0,"description":""}]""

bdlukaa commented 2 years ago

Maybe .contains('items', poll.items.map((i) => json.encode(i.toJson())).toList());?

Aleclock commented 2 years ago

I get the same error, I don't know why in this way seems to be a map instead as a list

"Error: DbRequest Instance of 'DbRequest' malformed array literal: "{"{"player":{"id":"409","name":"INSIGNE","team":"Napoli"},"votes":0,"description":""}","{"player":{"id":"2530","name":"IBRAHIMOVIC","team":"Milan"},"votes":0,"description":""}"}""