diff --git a/dialect/sql/schema/postgres_test.go b/dialect/sql/schema/postgres_test.go index d11bd78a7..1f075159f 100644 --- a/dialect/sql/schema/postgres_test.go +++ b/dialect/sql/schema/postgres_test.go @@ -57,12 +57,11 @@ func TestPostgres_Create(t *testing.T) { {Name: "id", Type: field.TypeInt, Increment: true}, }, Columns: []*Column{ - {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "id", Type: field.TypeUUID, Default: "uuid_generate_v4()"}, {Name: "name", Type: field.TypeString, Nullable: true}, {Name: "age", Type: field.TypeInt}, {Name: "doc", Type: field.TypeJSON, Nullable: true}, {Name: "enums", Type: field.TypeEnum, Enums: []string{"a", "b"}, Default: "a"}, - {Name: "uuid", Type: field.TypeUUID, Default: "uuid_generate_v4()"}, {Name: "price", Type: field.TypeFloat64, SchemaType: map[string]string{dialect.Postgres: "numeric(5,2)"}}, {Name: "strings", Type: field.TypeOther, SchemaType: map[string]string{dialect.Postgres: "text[]"}, Nullable: true}, }, @@ -71,7 +70,7 @@ func TestPostgres_Create(t *testing.T) { before: func(mock pgMock) { mock.start("120000") mock.tableExists("users", false) - mock.ExpectExec(escape(`CREATE TABLE IF NOT EXISTS "users"("id" bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL, "name" varchar NULL, "age" bigint NOT NULL, "doc" jsonb NULL, "enums" varchar NOT NULL DEFAULT 'a', "uuid" uuid NOT NULL DEFAULT uuid_generate_v4(), "price" numeric(5,2) NOT NULL, "strings" text[] NULL, PRIMARY KEY("id"))`)). + mock.ExpectExec(escape(`CREATE TABLE IF NOT EXISTS "users"("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "name" varchar NULL, "age" bigint NOT NULL, "doc" jsonb NULL, "enums" varchar NOT NULL DEFAULT 'a', "price" numeric(5,2) NOT NULL, "strings" text[] NULL, PRIMARY KEY("id"))`)). WillReturnResult(sqlmock.NewResult(0, 1)) mock.ExpectCommit() }, diff --git a/entc/gen/type.go b/entc/gen/type.go index 4e7c485e3..75fc73c65 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -1198,6 +1198,11 @@ func (f Field) PK() *schema.Column { c.Size = *f.def.Size } } + // Override the default-value defined in the + // schema if it was provided by an annotation. + if ant := f.EntSQL(); ant != nil && ant.Default != "" { + c.Default = strconv.Quote(ant.Default) + } if f.def != nil { c.SchemaType = f.def.SchemaType }