graphile / crystal

🔮 Graphile's Crystal Monorepo; home to Grafast, PostGraphile, pg-introspection, pg-sql2 and much more!
https://graphile.org/
Other
12.62k stars 571 forks source link

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

Closed cschroeter closed 7 years ago

cschroeter commented 7 years ago

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.

benjie commented 7 years ago

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 commented 7 years ago

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

cschroeter commented 7 years ago

Works with @next not with @latest :)

benjie commented 7 years ago

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

cschroeter commented 7 years ago

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

benjie commented 7 years ago

👍 Closing as fixed in v4.

magom001 commented 7 years ago

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.

benjie commented 7 years ago

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.

magom001 commented 7 years ago

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

jamesho2877 commented 5 years ago

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

benjie commented 5 years ago

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.

korenzerah commented 5 years ago

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

benjie commented 5 years ago

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