go-gorm / go-gorm/postgres

AutoMigrate error:

Open
#307 0 comments 0 reactions 1 assignee Claimed by @jinzhu View on GitHub
type:question
Dominant language
Go
Stars
297
Forks
155
Avg merge
27m
Merged PRs (30d)
2

Description

## 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.DeletedAt `json:"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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.