Graphql-engine: Empty string as default value for text fields

Created on 8 May 2019  路  13Comments  路  Source: hasura/graphql-engine

I like to avoid nullable fields so i don't also need to check for the null case every time in type safe languages.

But i often need to add new (non nullable) text field to existing table with data. I want the field to be initialized as empty string for the existing rows.

I have been doing this by adding any text string as the default value and then changing it to empty string. But would be better if you could specify empty string as the default value.

console easy enhancement high

Most helpful comment

Similiar to @elitan's workaround, it is also possible to modify an already existing column:

ALTER TABLE persons ALTER COLUMN first_name SET DEFAULT '';

It is correctly detected in the Hasura Console afterwards.

All 13 comments

Same issue here. I added the column via SQL instead.

ALTER TABLE "users" ADD COLUMN "name" text NOT NULL DEFAULT '';

This can be fixed by changing the way console accepts default string values. The following options seem possible:

  • we can make string values need to be explicitly surrounded by 's to avoid confusion and allow same flexibility as SQL

    • issue: unintuitive

    • issue: if quotes are missed, the error msg by PG is very unhelpful

  • we treat '' as a special input for empty string

    • issue: how to set default value to the string ''

    • issue: how does user know '' means empty string without just trying it out

Any other suggestions would be welcome as well

Also, extending this issue to also allow setting a default value with 's in them which currently is not possible

What is the problem to do the following?

  • if default value text box is empty, the default value will be empty (i.e '').

@elitan in that case it would not be possible to not set a default value. We'll then need to add a checkbox to mark that a default has to be set (that's what adminer does).

It is definitely another valid option for sure.

Here is some inspiration from how phpmyadmin handles it:

image


image


image

Very intuitive I think.

Use trim('') or format('') as the default value in the UI -- either are easily recognizable as string functions.

@aerskine this is not currently support right?

Similiar to @elitan's workaround, it is also possible to modify an already existing column:

ALTER TABLE persons ALTER COLUMN first_name SET DEFAULT '';

It is correctly detected in the Hasura Console afterwards.

@franciscolourenco No not supported directly from UI, only via SQL -- but @fpieper or @elitan have a better approach, I didn't realize when I posted that '' would be correctly displayed in Console

Why not simply: if the default value is surrounded by either " or ', remove them and take whatever's between, which could be an empty string.
Anyone who tries to set an empty string default value will certainly first try "" or ''.

Screen Shot 2020-06-04 at 9 13 19 AM
it would be a beautiful and very useful approach.

For if someone is as noob as I was when applied @fpieper workaround.

Here is the documentation for sending post values to hasura:
https://hasura.io/docs/1.0/graphql/manual/api-reference/schema-metadata-api/run-sql.html

Ran my post with postman and it worked perfectly, had to send headers
Content-Type: application/json
x-hasura-admin-secret: ** (my admin secret)

and body raw:

{
    "type": "run_sql",
    "args": {
        "sql": "ALTER TABLE properties ALTER COLUMN colonia SET DEFAULT '';"
    }
}

@binaryme I am not sure if you needed to run the query from a client or programmatically, but you can easily run the query from the Hasura Console (under Data / SQL (last point in the left sidebar)).

That's the way I applied my query :)

Was this page helpful?
0 / 5 - 0 ratings