1
votes

I have a gorm model:

type Order struct {
    ID             int    `json:"id" gorm:"column:id"`
    ProductID      int    `json:"product_id" gorm:"column:product_id"`
    Price int    `json:"per_credit_price" gorm:"column:per_credit_price"`

    Product Product `gorm:"foreignkey:ProductID;AssociationForeignKey:ID"`
}

And i`d like to write a query, like:

p.DB.Preload("Product").Where(ord).First(ord).Error

if my ord struct contains the Product as struct, it returns with the following error:

sql: converting argument $8 type: unsupported type models.Product, a struct

It would work find Find() instead of Where(), but I'd like to query for others than ID.

How could I make it work?

1
just pass id in where no need pass full struct data - Eklavya

1 Answers

0
votes

use pointer of the struct to do the find

var ord Order 
p.DB.Preload("Product").Where(ord).First(&ord).Error