我正在使用grpc构建rest 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 -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引发错误:即使“id”实际上不为空,“id”列中的空值也违反了非空约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62950811/

10-14 14:44
查看更多