GORM Playground Link
Description
ERROR: duplicate key value violates unique constraint "reports_pkey"
code:
type report struct {
gorm.Model
Name string `gorm:"column:name" json:"name"`
}
func TestPgBug(t *testing.T) {
dsn := fmt.Sprintf("host=%s user=%s port=%d dbname=%s password=%s sslmode=disable",
"127.0.0.1", "postgres", 15432, "test", "123456")
log.Println(dsn)
pgDb, err := gorm.Open(postgres.New(postgres.Config{
DSN: dsn,
PreferSimpleProtocol: true,
}))
if err != nil {
t.Fatal(err)
}
pgDb = pgDb.Debug()
pgDb.AutoMigrate(&report{})
test1 := &report{Name: "test1,不插入id"}
test2 := &report{Name: "test2,插入id", Model: gorm.Model{ID: 2}}
test3 := &report{Name: "test3,不插入id"}
if err := pgDb.Create(&test1).Error; err != nil {
t.Fatal(err)
}
if err := pgDb.Create(&test2).Error; err != nil {
t.Fatal(err)
}
if err := pgDb.Create(&test3).Error; err != nil {
t.Fatal(err)
}
}
=== RUN TestPgBug
2024/06/12 16:24:31 host=127.0.0.1 user=postgres port=15432 dbname=test password=123456 sslmode=disable
2024/06/12 16:24:31 /home/sin/projects/worframe/test/gorm_test.go:28
[6.129ms] [rows:1] SELECT count(*) FROM information_schema.tables WHERE table_schema = CURRENT_SCHEMA() AND table_name = 'reports' AND table_type = 'BASE TABLE'
2024/06/12 16:24:31 /home/sin/projects/worframe/test/gorm_test.go:28
[7.617ms] [rows:0] CREATE TABLE "reports" ("id" bigserial,"created_at" timestamptz,"updated_at" timestamptz,"deleted_at" timestamptz,"name" text,PRIMARY KEY ("id"))
2024/06/12 16:24:31 /home/sin/projects/worframe/test/gorm_test.go:28
[2.806ms] [rows:0] CREATE INDEX IF NOT EXISTS "idx_reports_deleted_at" ON "reports" ("deleted_at")
2024/06/12 16:24:31 /home/sin/projects/worframe/test/gorm_test.go:32
[2.288ms] [rows:1] INSERT INTO "reports" ("created_at","updated_at","deleted_at","name") VALUES ('2024-06-12 16:24:31.789','2024-06-12 16:24:31.789',NULL,'test1,不插入id') RETURNING "id"
2024/06/12 16:24:31 /home/sin/projects/worframe/test/gorm_test.go:35
[1.768ms] [rows:1] INSERT INTO "reports" ("created_at","updated_at","deleted_at","name","id") VALUES ('2024-06-12 16:24:31.791','2024-06-12 16:24:31.791',NULL,'test2,插入id',2) RETURNING "id"
2024/06/12 16:24:31 /home/sin/projects/worframe/test/gorm_test.go:38 ERROR: duplicate key value violates unique constraint "reports_pkey" (SQLSTATE 23505)
[1.340ms] [rows:0] INSERT INTO "reports" ("created_at","updated_at","deleted_at","name") VALUES ('2024-06-12 16:24:31.793','2024-06-12 16:24:31.793',NULL,'test3,不插入id') RETURNING "id"
gorm_test.go:39: ERROR: duplicate key value violates unique constraint "reports_pkey" (SQLSTATE 23505)
--- FAIL: TestPgBug (0.08s)
FAIL
This Bug could come from a problem with the database itself
create table test_table
(
id bigserial
primary key,
name varchar
);
alter table test_table
owner to postgres;
insert into test_table(name) values ('test1,no insert id');
insert into test_table values (2,'test2,insert id');
insert into test_table(name) values ('test3,no insert id');
The same problem arises ``` [23505] ERROR: duplicate key value violates unique constraint "test_table_pkey" 详细:Key (id)=(2) already exists.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨