gpt4 book ai didi

postgresql - postgres 抛出错误 : null value in column "id" violates not-null constraint even when value is actually not null

转载 作者:行者123 更新时间:2023-12-01 22:21:52 25 4
gpt4 key购买 nike

我正在使用 grpc 构建一个休息 api,但即使我填充了值,我也会收到上述错误。
curl Post 被重定向到 createAlbum 方法
我确实在 postgres 中手动创建了一个数据库和表
我的数据库名称是专辑我使用 psql 专辑登录(没有给出密码)
然后我创建了

create Table PHOTO(id INTEGER PRIMARY KEY, albumId INTEGER NOT NULL, title VARCHAR(255) NOT NULL, url VARCHAR(255) UNIQUE NOT NULL, thubmNailUrl VARCHAR(255) UNIQUE NOT NULL);
这是我的主要代码
func main(){
prosgretConname := fmt.Sprintf("dbname=%v user=%v sslmode=disable", "album", "s")
log.Infof("conname is %s", prosgretConname)
db, err := gorm.Open("postgres", prosgretConname)
if err != nil {
panic("Failed to connect to database!")
}// connects correctly to db

//db.Table("photo")
defer db.Close()
go startGRPC(db)
go startHTTP()
// Block forever
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()

}
func startGRPC(db *gorm.DB) {
lis, err := net.Listen("tcp", "localhost:5566")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
srv := svc.NewAlbumServer(db)
albumgpb.RegisterAlbumServiceServer(grpcServer, srv)
...
}
现在我的 newAlbumServer 返回数据库的一个实例
type Album struct {
title string `json:"title"`
albumid int `json:"albumid"`
id int `json:"id" gorm:"primary_key;not null"`
url string `json:"url"`
thumbnailurl string `json:"thumbnailurl"`
}
func (a *AlbumServiceServer) CreateAlbum(ctx context.Context, in *albumgpb.Albumreq) (*albumgpb.Albumreq, error) {
tx := a.db.Begin()

photo := Album{}
photo.title = in.Album.Title
photo.albumid = int(in.Album.AlbumId)
photo.id = int(in.Album.Id)
photo.url = in.Album.Url
photo.thumbnailurl = in.Album.ThumbNailUrl

log.Infof("%v", photo.id) // this prints 1
log.Infof("the id is %v", int(in.Album.Id))// this prints 1 on server logs
log.Infof("%+v\n", in) //this prints the entire request in the POST command
//tx.Table("photo").

err := tx.Table("public.photo").Create(&photo).Error // fails in this line stating pq: null value in column "id" violates not-null constraint

if err != nil {
log.Errorf("could not insert into db")
tx.Rollback()
return nil, err
}

tx.Commit()
……
这是我的 curl 请求和响应
curl -X POST "http://localhost:8080/album" -d '{"id":1,"albumId":2,"title":"bleh","url":"sds","thumbNailUrl":"123"}'
2020-07-17 14:34:54.641 IST [16074] ERROR: null value in column "id" violates not-null constraint
2020-07-17 14:34:54.641 IST [16074] DETAIL: Failing row contains (null, null, null, null, null).
2020-07-17 14:34:54.641 IST [16074] STATEMENT: INSERT INTO "public"."photo" DEFAULT VALUES RETURNING "public"."photo".*
{"error":"pq: null value in column \"id\" violates not-null constraint","code":2,"message":"pq: null value in column \"id\" violates not-null constraint"}
任何关于它为什么失败或我做错了什么的建议都会很有帮助

最佳答案

请看this .
由于id字段未导出(以小写字母开头),json 均不可见包和gorm .

关于postgresql - postgres 抛出错误 : null value in column "id" violates not-null constraint even when value is actually not null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62950811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com