Postgres Error : failed to encode args[4]: unable to encode 0x1 into binary format for _int4 (OID 1007): cannot find encode plan

75 views Asked by At

So im creating a web server in golang where im working on an cart api function

func AddProductToCart(context *gin.Context) {
    // get payload from body

    var body struct {
        UserID    int
        ProductID int
    }

    err := context.ShouldBind(&body)

    if err != nil {
        context.JSON(http.StatusBadRequest, gin.H{
            "message": "Error binding body data",
        })
        return
    }

    log.Println(body.ProductID, body.UserID)

    if body.ProductID == 0 || body.UserID == 0 {
        context.JSON(http.StatusBadRequest, gin.H{
            "message": "Please send UserID and ProductID in payload",
        })
        return
    }

    var users models.User

    user := initializers.DB.Find(&users, "id =?", body.UserID)

    if user.Error != nil {
        context.JSON(http.StatusBadRequest, gin.H{
            "message": "Error quering user data to DB",
        })
        return
    }

    if user.RowsAffected == 0 {
        context.JSON(http.StatusBadRequest, gin.H{
            "message": "User with provided UserID not found",
        })
        return
    }

    var carts models.Cart

    result := initializers.DB.Find(&carts, "user_id =?", body.UserID)

    if result.Error != nil {
        context.JSON(http.StatusBadRequest, gin.H{
            "message": "Error quering cart data to DB",
        })
        return
    }

    if result.RowsAffected == 0 {

        var newCart models.Cart

        if newCart.Products == nil {
            newCart.Products = make([]int, 0)
        }

        newCart.UserID = body.UserID
        newCart.Products = append(newCart.Products, body.ProductID)

        result := initializers.DB.Create(&newCart)

        if result.Error != nil {
            context.JSON(http.StatusBadRequest, gin.H{
                "message": "Unable to save cart to db",
            })
            return
        }

        context.JSON(http.StatusOK, gin.H{
            "message": "Product added to cart successfully",
        })

    } else {

        var newCart models.Cart = carts

        newCart.Products = append(newCart.Products, body.ProductID)

        result := initializers.DB.Save(&newCart)

        if result.Error != nil {
            context.JSON(http.StatusBadRequest, gin.H{
                "message": "Unable to save cart to db",
            })
            return
        }

        context.JSON(http.StatusOK, gin.H{
            "message": "Product added to cart successfully",
        })

    }
}

So I am trying to insert an array of integers (productID's) to single column in Cart table.

        result := initializers.DB.Create(&newCart)

        if result.Error != nil {
            context.JSON(http.StatusBadRequest, gin.H{
                "message": "Unable to save cart to db",
            })
            return
        }

so at this part of code there is an error in terminal that says

GO-Backend/Go-Shopping-backend/controller/cart.go:124 failed to encode args[4]: unable to encode 0x1 into binary format for _int4 (OID 1007): cannot find encode plan

[8.885ms] [rows:0] INSERT INTO "carts" ("created_at","updated_at","deleted_at","user_id","products") VALUES ('2024-03-09 00:46:58.831','2024-03-09 00:46:58.831',NULL,2,(1)) RETURNING "id","products"

So can anyone help me what can be the issue ???

For context im using gorm , and gin. And im also providing the models for user and cart.

type Cart struct {
    gorm.Model
    UserID   int
    Products []int `gorm:"type:integer[];default:'{}'"`
}
type User struct {
    gorm.Model
    Email    string `gorm:"unique"`
    Password string
    Role     int  `gorm:"default:2"`
    CartID   uint // Foreign key for the Cart
}

So i want the products array to be saved in DB.

0

There are 0 answers