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

Uncaught error when retrieving columns info with jsonb[] with default values #72

Closed
ntma opened this issue Oct 14, 2021 · 9 comments · May be fixed by #73
Closed

Uncaught error when retrieving columns info with jsonb[] with default values #72

ntma opened this issue Oct 14, 2021 · 9 comments · May be fixed by #73

Comments

@ntma
Copy link

ntma commented Oct 14, 2021

Hi knex-schema-inspector team,

Due to the nature of this regex, it seems that the default value of columns defined as json[] or jsonb[] will be parsed as Json objects and throw an uncaught error.

Reproduction

  • define a table with the following column;
   my_column jsonb[] default ARRAY[]::jsonb[],
  • retrieve information for the columns in our table;

In this case, the parsing will look like JSON.parse("ARRAY[]"), which is not a valid format.

@ntma
Copy link
Author

ntma commented Oct 14, 2021

As discussed in this thread, we could wrap the parsing with a try/catch to avoid stopping the execution unexpectedly.

I don't mind doing this change, but I need to understand if:

  • A - we should catch the error and return the default value as a string;
  • B - write code to start parsing array's as default values;

@rijkvanzanten
Copy link
Collaborator

Heya! Me again 👋🏻

What is the "raw" javascript representation of a default value as returned by pg in this case? Is it a buffer, or a string representation?

@ntma
Copy link
Author

ntma commented Oct 15, 2021

Hey there,

So, their documentation is a bit incomplete:
    https://node-postgres.com/api/types

But their code shows that the text value of json[]/jsonb[] (postgres representation) is parsed into an array of json's (javascript representation).

    https://github.com/brianc/node-pg-types/blob/8594bc6befca3523e265022f303f1376f679b5dc/lib/textParsers.js#L179-L180

@rijkvanzanten
Copy link
Collaborator

Hmm odd, that makes me wonder why the parse default value step fails then. Technically speaking, passing an array of JS objects to JSON.parse() shouldn't crash like that 🤔

@ntma
Copy link
Author

ntma commented Oct 15, 2021

But in this case, the default value that we are trying to parse is ARRAY[]. JSON.parse is allergic to anything that doesn't start with a {

@rijkvanzanten
Copy link
Collaborator

@ntma Oh I see, when you mentioned:

But their code shows that the text value of json[]/jsonb[] (postgres representation) is parsed into an array of json's (javascript representation).

I thought you meant that it would be returned as actual valid JSON (as ARRAY[] isn't actually JSON ofc)

@ntma
Copy link
Author

ntma commented Oct 17, 2021

I am sorry for the confusion @rijkvanzanten .

TLDR we have in one side the parsing that the pg module does to retrieve values from the database, which I believe is not related to this issue.

On the other side we have the parsing that knex-schema-inspector does to extract information from schema definitions. And if I am not wrong, currently this parsing does not support the PostgreSQL syntax for array's as default values (since there is no direct mapping to JavaScript for Array[]).

@aidenfoxx
Copy link
Contributor

@rijkvanzanten Isn't this (kinda) fixed with #117 since we now return unparsed JSON strings?

@rijkvanzanten
Copy link
Collaborator

Yes!

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

Successfully merging a pull request may close this issue.

3 participants