Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres (SQLSTATE 23505) Error #741

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

haiziohhue
Copy link

Explain your user case and expected results

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)
	}
}
testLog:
```sh
=== 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant