i have this model data structure:
type Order struct {
OrderEntry OrderEntry `db:"order"`
}
type OrderEntry struct {
Order_uid string
Track_number string
Entry string
Delivery Delivery
Payment Payment
Items []Item
Locale string
Internal_signature string
Customer_id string
Delivery_service string
Shardkey string
Sm_id int64
Date_created string
Oof_shard string
}
type Delivery struct {
...
}
type Payment struct {
...
}
type Item struct {
...
}
And i have table
CREATE TABLE "order"
(
"order" jsonb NOT NULL
);
How do i insert Order object to this psq table? I use sqlx and for this code im getting error
func (r *RepositoryPostgres) CreateDocument(order L0.Order) error {
tx := r.db.MustBegin()
tx.MustExec("INSERT INTO order (order) VALUES ($1)", order.OrderEntry)
err := tx.Commit()
if err != nil {
return err
}
return nil
}
panic: sql: converting argument $1 type: unsupported type L0.OrderEntry, a struct
How to properly fix this? Thanks
2
Answers
The INSERT statement’s unsupported type "L0.OrderEntry" is the cause of the issue you’re experiencing. "L0.OrderEntry" cannot be directly entered into the JSONB column since it is a nested struct within "L0.Order". Instead, before entering the OrderEntry object into the table, you must marshal it into a JSON representation.
You can change your code as below to resolve the error.
The order.OrderEntry object is marshalled into JSON in the modified code using the "json.Marshal" method. The generated JSON byte slice (orderJSON) is then provided to the INSERT statement as the value.
You may make sure the data is in the correct format for insertion into the JSONB column by marshalling the nested struct into JSON.
There are a couple of changes you need to make here.
This will now store your struct as a JSON byte-array in the table.
You can name these however you like, snake-case being the common format. These tags provide the names for the JSON fields to the parser.
Without these fields, you’ll end up with empty JSON, and you won’t be able to retrieve the data.