crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Default value "now()" is not applied on create, column set to null

Open cschroeter opened this issue 9 years ago • 13 comments

Given the following schema:

create schema forum_example;

create table forum_example.user (
  id                     serial primary key,
  name               text not null check (char_length(name) < 80),
  created_at       timestamp default now() 
);

If I add a user like this

insert into forum_example.user (name) values ('Christian');

a valid user with an id, name and timestamp has been created. But If i use graphiql with this mutation

mutation {
  createUser(input: {user: {name: "Christian"}}) {
    user {
      id
      name
      createdAt
    }
  }
}

the createdAt field is null.

cschroeter avatar Sep 18 '17 15:09 cschroeter

Can you confirm which value of PostGraphQL you're using? Specifically I'm interested to know if the issue occurs in both @latest and @next or only one. You can run the specific versions via npx without messing up your environment; e.g.

npx postgraphql@latest -c postgres://...
npx postgraphql@next -c postgres://...

benjie avatar Sep 18 '17 15:09 benjie

(This seems to be kind of the opposite of #584)

benjie avatar Sep 18 '17 15:09 benjie

Works with @next not with @latest :)

cschroeter avatar Sep 18 '17 15:09 cschroeter

I'm probably not going to fix it then; but if you wish to open a PR to fix this in v3 please do.

More information on postgraphql@next is available in #506; it's still in alpha currently but is already quite widely used and seems stable (though new versions might bring small breaking changes until it goes on general release).

benjie avatar Sep 18 '17 15:09 benjie

Thanks for the fast response. I am fine with using @next version. Keep up the good work. It is an amazing library !

cschroeter avatar Sep 18 '17 15:09 cschroeter

👍 Closing as fixed in v4.

benjie avatar Sep 18 '17 16:09 benjie

Hi, I have the same issue. Using postgraphql 3.5.0 as a middleware. Column create_at default now() returns empty value when creating new rows from postgraphql.

arcadiy-magomedov avatar Sep 28 '17 07:09 arcadiy-magomedov

I'd be happy to merge a PR that fixes this for v3. If you're happy running a late alpha, you can also try v4 with npm install postgraphql@next (see #506) but beware that (subtle) schema changes are still possible at this stage.

benjie avatar Sep 28 '17 08:09 benjie

Sorry, Benjie! Just figured it out. Removed postgraphql from dependencies and installed postgraphile. Now it works like a charm.

arcadiy-magomedov avatar Sep 28 '17 09:09 arcadiy-magomedov

This works fine with the middleware but has not been applied for the CLI though.

jamesho2877 avatar Feb 10 '19 15:02 jamesho2877

What do you mean? The “postgraphile” CLI just calls the “postgraphile” middleware. If you think you’ve found a bug, please open a new issue.

benjie avatar Feb 10 '19 16:02 benjie

Hello, Is there a way to apply now() on a date time field on update without creating a trigger?

korenzerah avatar Mar 06 '19 17:03 korenzerah

Trigger is the cleanest way to do it, but you could do something like this: https://www.graphile.org/postgraphile/plugin-gallery/#Mutation_wrappers__OverrideArgValuePlugin

benjie avatar Mar 06 '19 20:03 benjie