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 #7062

Open
haiziohhue opened this issue Jun 12, 2024 · 1 comment
Open

postgres (SQLSTATE 23505) Error #7062

haiziohhue opened this issue Jun 12, 2024 · 1 comment
Assignees
Labels

Comments

@haiziohhue
Copy link

haiziohhue commented Jun 12, 2024

GORM Playground Link

palyground 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.
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Jun 12, 2024
Copy link

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.ioSearch Before Asking

@github-actions github-actions bot added status:stale type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants