AutoMigrate error:

Open
#307 0 comments 0 reactions 1 assignee View on GitHub

@jinzhu is already working on this.

Since Apr 29, 2025.

Assessment

This issue has not been assessed yet.

Description

type:question

Your Question

The following SQL queries are executed:

`SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = $1 AND table_type = 'BASE TABLE' AND table_schema = CURRENT_SCHEMA()
terminals

SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_name = $1 AND column_name = $2 AND table_schema = CURRENT_SCHEMA()
terminals id

SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_name = $1 AND column_name = $2 AND table_schema = CURRENT_SCHEMA()
terminals version

SELECT * FROM "terminals" WHERE (deleted_at IS NULL) ORDER BY created_at DESC LIMIT 1`

The versions I am using are as follows:

github.com/lib/pq v1.10.9

gorm.io/driver/postgres v1.5.11

gorm.io/gorm v1.26.0

The structure is as follows:
type Terminal struct { ID int json:"id" gorm:"primaryKey"Version int json:"version" gorm:"default:1" comment:"协议版本号"Mac string gorm:"index" comment:"终端mac地址"MachineID string gorm:"unique;default:''" json:"machine_id" comment:"终端机器码"IP string json:"ip" comment:"终端IP"Os string json:"os" comment:"终端系统信息"ExpiredAt time.Time json:"expired_at" comment:"过期时间"CreatedAt time.Time json:"created_at" comment:"注册时间"UpdatedAt time.Time json:"updated_at" comment:"最后登录时间"DeletedAt gorm.DeletedAtjson:"deleted_at" gorm:"index"}
Table schema:
Table "public.terminals" Column | Type | Collation | Nullable | Default ------------+--------------------------+-----------+----------+--------------------------------------- id | integer | | not null | nextval('terminals_id_seq'::regclass) version | integer | | | 1 mac | text | | | machine_id | text | | | ip | text | | | os | text | | | expired_at | timestamp with time zone | | | created_at | timestamp with time zone | | | updated_at | timestamp with time zone | | | deleted_at | timestamp with time zone | | | Indexes: "terminals_pkey" PRIMARY KEY, btree (id) "terminals_machine_id_key" UNIQUE CONSTRAINT, btree (machine_id) "idx_terminals_deleted_at" btree (deleted_at) "idx_terminals_mac" btree (mac) "terminals_mac_key" btree (mac)
In the Go code, the following function is executed:
db.AutoMigrate(Terminal{})
Tracing leads to the function in github.com/lib/pq/conn.go:
func (cn *conn) query(query string, args []driver.Value) (_ *rows, err error) {}
The SQL query generated is SELECT * FROM "terminals" LIMIT $1, but the args array contains two parameters: 5 and 1.

Therefore, the error occurs: "got %d parameters but the statement requires %d".

Please help me find out where my problem lies

The document you expected this should be explained

Expected answer

Dominant language
Go
Stars
298
Forks
158
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from go-gorm/postgres

All issues in go-gorm/postgres

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.