Your Question

How can we upsert association with conditions? If the main record condition does not meet, associations should not be saved.

The document you expected this should be explained

https://gorm.io/docs/associations.html

Expected answer

Additional flag to enable save associations based on parent entity.


Better context

Here's an example of what I want to achieve:

type User struct {
  ID string
  Pets []Pet
  UpdatedAt time.Time
}

type Pet struct {
  ID string
  Name string
}


// conditions
tx.clause.OnConflict{
    Where: clause.Where{
        Exprs: []clause.Expression{
            clause.Lte{
                Column: fmt.Sprintf("%s.updated_at", user.TableName()),
                Value:  user.GetModifiedTs(),
            },
        },
    },
    UpdateAll: true,
},

I want to upsert User (and its associated entities) if only User.UpdatedAt is greater than what I have in database. Using this &gorm.SessionFullSaveAssociations: true}, it naively upserts to database even when the main entity condition (updatedAt is not greater than db record) has not met. Is there a way I can upsert associated entities only when parent entity condition is met.

Thanks