Home >Backend Development >Golang >Gorm creates form data file upload error
php editor Youzi today introduces to you the problem of uploading error in Gorm creation form data file. During the development process, we often encounter the need to upload files, and Gorm is a powerful ORM library that provides convenient database operation methods. However, when using Gorm for form data file upload, some errors sometimes occur. This article will analyze these errors for you and provide corresponding solutions to help you better deal with this problem.
I am trying to create records in postgresql server. The request is sent to me as file data in a multipart format. After uploading the file to my side, I call gorm.create but it throws an error.
When I comment out the file upload part, the error disappears, but I need to upload the file.
This is my controller part:
func (pc productcontroller) create(c *gin.context) { var product migrations.product if err := c.bind(&product); err != nil { c.json(400, gin.h{"error": err.error(), "message": "İşlem başarısız. lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-01"}) return } if product.name == "" { c.json(400, gin.h{"error": "name is required", "message": "İşlem başarısız. lütfen ad alanını boş bırakmayınız. hata kodu: pd-crt-02"}) return } if product.price == 0 { c.json(400, gin.h{"error": "price is required", "message": "İşlem başarısız. lütfen fiş değeri alanını boş bırakmayınız. hata kodu: pd-crt-03"}) return } if product.id != 0 { c.json(400, gin.h{"error": "remove id field", "message": "lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-id-01"}) return } file, err := c.formfile("image") if err != nil { c.json(400, gin.h{"error": err.error(), "message": "lütfen resim ekleyiniz. hata kodu: pd-crt-img-01"}) } filename := time.now().format("20060102150405") + "-" + strings.split(file.filename, ".")[0] + "." + strings.split(file.filename, ".")[1] dst := fmt.sprintf("./public/images/%s", filename) err = c.saveuploadedfile(file, dst) if err != nil { c.json(400, gin.h{"error": err.error(), "message": "lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-img-02"}) return } product.image = &migrations.file{ path: filename, extension: strings.split(file.filename, ".")[1], } log.println(product) err = db.conn.create(&product).error if err != nil { c.json(400, gin.h{"error": err.error(), "message": "İşlem başarısız. lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-04"}) return } c.json(http.statuscreated, gin.h{"message": "Ürün başarıyla eklendi.", "data": product}) return }
my request:
mistake:
{ "error": "strconv.parseint: parsing \"products\": invalid syntax; strconv.parseint: parsing \"products\": invalid syntax", }
This is my structure:
type Order struct { ID uint `gorm:"primarykey" json:"id"` UserID int `gorm:"index" json:"user_id"` RoomNo int `gorm:"comment:oda_no" json:"room_no"` IsDone bool `gorm:"default:false" json:"is_done"` StatusCode int `gorm:"default:0" json:"status_code"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt time.Time `gorm:"index" json:"deleted_at"` Products []*Product `gorm:"many2many:orders_products" json:"products,omitempty"` } type Product struct { ID uint `gorm:"primarykey" json:"id" form:"id"` Name string `gorm:"type:varchar(255)" json:"name" form:"name"` Price float64 `gorm:"type:decimal(10,2)" json:"price" form:"price"` IsActive bool `gorm:"default:true" json:"is_active" form:"isActive"` Image File `gorm:"polymorphic:Module" json:"image,omitempty"` CreatedAt time.Time `json:"created_at" form:"createdAt"` UpdatedAt time.Time `json:"updated_at" form:"updatedAt"` DeletedAt time.Time `gorm:"index" json:"deleted_at"` } type OrdersProduct struct { OrderID int `gorm:"index" json:"order_id"` ProductID int `gorm:"index" json:"product_id"` Count int `gorm:"default:0" json:"count"` } type File struct { ID uint `gorm:"primarykey" json:"id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt time.Time `gorm:"index" json:"deleted_at"` Path string `gorm:"type:varchar(255)" json:"path"` Extension string `gorm:"type:varchar(255)" json:"extension"` ModuleID int `gorm:"type:integer" json:"module_id"` ModuleType int `gorm:"type:integer" json:"module_type"` }
Check the unit type of the file structure. strconv.ParseInt() Converts a string to a value. I think ModuleID, ModuleType or both must be strings.
The above is the detailed content of Gorm creates form data file upload error. For more information, please follow other related articles on the PHP Chinese website!