graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

Empty string as default value for text fields

Open karibertils opened this issue 5 years ago • 14 comments

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.

karibertils avatar May 08 '19 15:05 karibertils

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

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

elitan avatar Aug 22 '19 05:08 elitan

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

rikinsk-zz avatar Aug 22 '19 10:08 rikinsk-zz

What is the problem to do the following?

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

elitan avatar Aug 22 '19 11:08 elitan

@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.

rikinsk-zz avatar Aug 23 '19 07:08 rikinsk-zz

Here is some inspiration from how phpmyadmin handles it:

image


image


image

Very intuitive I think.

elitan avatar Aug 23 '19 07:08 elitan

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

aerskine avatar Jan 08 '20 21:01 aerskine

@aerskine this is not currently support right?

franciscolourenco avatar Feb 07 '20 14:02 franciscolourenco

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.

fpieper avatar Feb 10 '20 13:02 fpieper

@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

aerskine avatar Feb 10 '20 19:02 aerskine

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 ''.

namesphill avatar Mar 04 '20 17:03 namesphill

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

serudda avatar Jun 04 '20 14:06 serudda

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 avatar Jun 10 '20 15:06 binaryme

@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 :)

fpieper avatar Jun 10 '20 16:06 fpieper

Hi folks,

I got the ball rolling for this one at https://github.com/hasura/graphql-engine/pull/7452

fhas

Implemented the solution suggested by @elitan and @sruda, what do you think about this solution @rikinsk ?

P.S: Another hack for now, if you are using PostgreSQL, just typing value ''::text will get the same thing done! :smile:

atb00ker avatar Aug 24 '21 14:08 atb00ker