type User struct {
  gorm.Model
  Languages []Language `gorm:"many2many:user_languages;"`
}

type Language struct {
  gorm.Model
  Name string
}

When I use now a Preload, gorm first fetches the entries in the right order from the JoinTable. But then gets the real data from the Target one which leads into that the ordering isn to preserved.

Problem is here that it does a SELECT * FROM languages WHERE name IN ('de', 'en'...)

From my pov this is a bug since I don't have even a chance to sort by a custom JoinTable then.

If this is intended, do you guys know a workaround how I preserve here on Preloading the INSERT order?

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

duplicated of https://github.com/go-gorm/gorm/issues/5229

    DB.Preload("Languages", func(tx *gorm.DB) *gorm.DB {
        return tx.Order("Name DESC")
    }).Find(&user1)

Comment From: roberth1988

@a631807682 Sorry but this is not the solution. It simply doesn't work because the Insert order that is out of interest is from the user_languages table and not from the Target one.

DB.Preload("Languages", func(tx *gorm.DB) *gorm.DB {
        return tx.Order("Name DESC")
    }).Find(&user1)

Does only going to: SELECT * FROM languages WHERE name IN ('de', 'en'...) ORDER by Name and its nothing about the Insert order within user_languages

Comment From: a631807682

It will be in user_languages primary key order, if no order is specified. the order of user_languages or the query of fields are not supported at present. Maybe we should support it, link https://github.com/go-gorm/gorm/issues/5321 @jinzhu

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking