1

So I have this table:

CREATE TABLE customer (
    id SERIAL PRIMARY KEY,
    name character varying(30) NOT NULL,
    created_at timestamp with time zone NOT NULL DEFAULT NOW()
);

And I have github.com/jackc/pgx/v4 in my import statement.

My Go code looks like this:

    conn, err := pgx.Connect(context.Background(), os.Getenv("postgres://user:@127.0.0.1:5432/dbname"))
    if err != nil {
        fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
        os.Exit(1)
    }
    defer conn.Close(context.Background())

    _, err = conn.Exec(context.Background(), "INSERT INTO customer(name) values($1)", "something")
    if err != nil {
        log.Fatal(err)
    }

Output:

2019/08/14 09:24:26 ERROR: null value in column "created_at" violates not-null constraint (SQLSTATE 23502)
exit status 1

How come when I have DEFAULT NOW()? I tried creating a recored from my DB GUI client (Postico) and created_at field got auto-populated.

What's wrong?

Adam
  • 2,948
  • 10
  • 43
  • 74
  • 2
    My guess is that `NULL` is being explicitly passed rather than implicitly. – J Spratt Aug 14 '19 at 14:03
  • Does that mean I can't depend on default SQL values when using pgx?! – Adam Aug 14 '19 at 14:25
  • @AdamSilver did you try a version that's not "under development", e.g. non-v4? – mkopriva Aug 14 '19 at 14:26
  • Is it possible to enable dumping of SQL statements coming in in your Postgres instance? (Or for a specific connection, or client)? I'd just peer at the log to be sure what gets sent. Maybe the `pgx` package has a similar debugging facility? I mean, @JSpratt had most probably guessed correctly: `pgx` explicitly sends `NULL` for every parameter you did not specify. – kostix Aug 14 '19 at 15:23

0 Answers0