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

Strings not being stringified when used with a JSONB column on PGSQL using jsonAttributes #1673

Closed
dinodeSimon opened this issue Feb 6, 2020 · 1 comment · Fixed by #2398
Assignees
Labels

Comments

@dinodeSimon
Copy link

Hi,

We are receiving a invalid input syntax for type json error when inserting a string value into a JSONB column on PGSQL using upsertGraph with a payload like:
{ jsonField: "value" }

Similar to this comment here it seems that if a plain string is used as a value for a JSONB column on PGSQL an error is generated even if the property is in jsonAttributes

#452 (comment)

It looks like jsonAttributes doesn't stringify the string because it isn't detected as an Object

if (isObject(value)) {

To work around this we can do something like this which works but it would be nice to know if there is a supported way or if we are missing something obvious

$beforeInsert() { if (this.jsonField) { this.jsonField = JSON.stringify(this.jsonField); } }

Thanks for building such an awesome orm. Let me know if you need more details or more detailed steps to repeat.

@lehni lehni self-assigned this Apr 14, 2023
@lehni
Copy link
Collaborator

lehni commented Apr 15, 2023

It looks like this was intended by @koskimas, see #452 (comment)

But there is a contraction in the current behavior in the symmetry between parseJsonAttributes() that always parses the string value and formatJsonAttributes() that only formats if it's an object.

So if you have a JSON string of a JS string in your DB ('"some string"'), reading it will give you a proper JS string as expected ('some string'), but storing this again will now store an invalid JSON string ("some string").

I think removing the isObject() check is the right thing to do, and it will allow people to store all kinds of JSON values, not just objects and arrays.

lehni added a commit that referenced this issue Apr 15, 2023
lehni added a commit that referenced this issue Apr 15, 2023
lehni added a commit that referenced this issue Apr 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants