Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON lists not working correctly #35

Open
christian-thiele opened this issue Apr 19, 2022 · 0 comments
Open

JSON lists not working correctly #35

christian-thiele opened this issue Apr 19, 2022 · 0 comments

Comments

@christian-thiele
Copy link

When querying a table with a jsonb column that contains a json list, the result is parsed correctly.
When trying to insert or update with the same list (the exact object that i received as result) I get this error:

PostgreSQLSeverity.error : Could not infer array type of value '[{key1: 123, key2: 456}]'.

I think the cause of this error is that the PostgresTextEncoder does not recognize the list as a JSON value:

if (value is Map) {
  return _encodeJSON(value, escapeStrings);
}

if (value is PgPoint) {
  return _encodePoint(value);
}

if (value is List) {
  return _encodeList(value);
}

(from text_codec.dart line 31 and following)

Also, there could be a bug in the _encodeList method here:

final type = value.fold(value.first.runtimeType, (type, item) {
  if (type == item.runtimeType) {
    return type;
  } else if ((type == int || type == double) && item is num) {
    return double;
  } else {
    return Map;
  }
});

(text_codec.dart line 176)

AFAIK comparing runtimeType is not advisable because (like in this scenario) it would result in type being _InternalLinkedHashMap<String, dynamic>. This would then clash with line 197 where type is compared to Map
but _InternalLinkedHashMap<String, dynamic> != Map. But since the syntax of the resulting sql would not fit for use with jsonb anyway, i think the first code part above is the root of my problem.

If you can give me some opinion about how this should be handled, i would be happy to contribute a PR for this.

Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant