Your Question

Assuming we have the following table created in a PostgreSQL database:

CREATE TABLE users
(
    id          BIGSERIAL    NOT NULL PRIMARY KEY,
    name        VARCHAR(64)  NOT NULL,
    email       VARCHAR(128) UNIQUE,
    avatar      VARCHAR(256) NOT NULL DEFAULT ''
);

We defined a memory model for it:

type User struct {
    ID        int64
    Name      string
    Avatar    string
    Email     string
}

We create a user like this with GORM:

user := &User {
    Name: "test"
}
_ = db.Create(p).Error

The generated SQL is like:

INSERT INTO "users" ("name","avatar","email") VALUES ('test','','') RETURNING "id"

It does not respect the DDL definition for zero-value fields.

The document you expected this should be explained

Expected answer

Is it possible to make it generate SQL like this:

INSERT INTO "users" ("name","avatar","email") VALUES ('test',DEFAULT,DEFAULT) RETURNING "id";
-- or
INSERT INTO "users" ("name") VALUES ('test') RETURNING "id";

Comment From: jinzhu

https://gorm.io/docs/create.html#Default-Values