Your Question

Can i filter preload inside children ?

My Model

type MasterLayanan struct {
    ID           int              `gorm:"primaryKey;column:id"`
    IDParent     int              `gorm:"column:id_parent"`
    Nama         string           `gorm:"column:nama"`
    Children     []MasterLayanan  `gorm:"foreignKey:IDParent"`
    HistoryPrice []HistoryLayanan `gorm:"foreignKey:IDLayanan"`
}

type HistoryLayanan struct {
    ID                 int    `gorm:"primaryKey;column:id"`
    IDLayanan          int    `gorm:"column:id_layanan"`
    TarifFixed         int    `gorm:"column:tarif_fixed"`
}
func List(...) (result []models.MasterLayanan, count int64, err error) {
    if tx == nil {
        tx = repo.Postgres
    }

    query := tx.WithContext(ctx).
        Debug().
        Model(models.NewMasterLayanan()).
        Where("id_parent IS NULL").
                Preload("HistoryPrice", "TarifFixed > ?", 100).
        Preload(clause.Associations, preload)

    err = query.Order(orderBy + " " + sort).Offset(offset).Limit(limit).Find(&result).Error
    return result, count, err
}

func preload(d *gorm.DB) *gorm.DB {
    return d.Preload(clause.Associations, preload)
}

The code above succeeds in getting the parent and child along with the HistoryPrice but how can I filter the HistoryPrice in the child?

The document you expected this should be explained

reference code : https://github.com/go-gorm/gorm/issues/4027

Expected answer

I can filter HistoryPrice which is in children

Comment From: github-actions[bot]

This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days