我想在使用Postgres的应用程序中声明我的Go模型。
我正在经历 go-pg
。及其相关标签。
但是在有关Model Definition的文档中,似乎没有要自动递增的字段(pk)的标签(?)(即SERIAL
sql定义中postgres
的对应内容,例如如下)
CREATE TABLE IF NOT EXISTS "day" (
"id" SERIAL PRIMARY KEY,
如何在
go
模型结构中声明这样的选项?
最佳答案
我没有使用过go-pg
,但据我所知,遍历文档ID
(或Id
)字段会自动解析为串行键和主键,因此您无需指定它。
从model definition docs:
type Genre struct {
Id int // Id is automatically detected as primary key
}
type Author struct {
ID int // both "Id" and "ID" are detected as primary key
}
关于postgresql - 串行(自动递增)主键的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59695259/