Postgraphile: how to create with serial primary key

Created on 2 Aug 2017  路  6Comments  路  Source: graphile/postgraphile

I'm trying to do the most simple create mutation.

mutation ($user:UserInput!) {
  createUser(input: {user:$user}) {
    user{
      id
      name
    }
  }
}

variables:

{"user": {"id": 1, "name": "John" }}

How can I omit the id field?

{"user": {"name": "John" }}

schema:

create table mydb.user (
  id              serial not null primary key,
  name            text not null
);

Most helpful comment

That's entirely a PostgreSQL concern; you'd have the same issue on the command line with psql

(But since you asked: you're looking for the ALTER SEQUENCE command)

To keep things manageable I'm going to close this issue as I think it's solved; but if not or you require further help please re-open it.

All 6 comments

@jmparsons If you've marked id as primary key and it has a default then you shouldn't need to specify id; if you're still struggling please provide your database schema (or at least the user table) and we'll try to help 馃憤

@benjie Hi I've updated my question with the schema. It's just two fields id and name.

@jmparsons You should be able to just omit the id... is it that you've set the variables to client instead of user?

If you look in GraphiQL you should see something like this:

screenshot 2017-08-02 00 24 17

The id: Int has no exclamation mark so is not required.

@benjie I got it to work, but I had to wipe my data. I have a data seed and the increment tries to use a key that's already be applied giving me "duplicate key value violates unique constraint \"user_pkey\"".

I guess is there a way to update the postgres cursor or something along those lines for seeded data?

That's entirely a PostgreSQL concern; you'd have the same issue on the command line with psql

(But since you asked: you're looking for the ALTER SEQUENCE command)

To keep things manageable I'm going to close this issue as I think it's solved; but if not or you require further help please re-open it.

@benjie You're the best!

Was this page helpful?
0 / 5 - 0 ratings