• 系统版本: mac os 14.4.1
  • gorm版本: v1.25.10
  • golang版本: 1.22.2
  • model 如下
type Artist struct {
    ID                   string         `gorm:"column:id;type:varchar(64);primaryKey" json:"id"`
    CreateAt             time.Time      `gorm:"column:create_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);comment:创建时间" json:"create_at"` // 创建时间
    UpdateAt             time.Time      `gorm:"column:update_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);comment:修改时间" json:"update_at"` // 修改时间
    DeletedAt            gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);comment:删除时间" json:"deleted_at"`                                     // 删除时间
    Photo                string         `gorm:"column:photo;type:varchar(1024);not null" json:"photo"`
    Name                 string         `gorm:"column:name;type:varchar(256);not null;index:idx_name,priority:1" json:"name"`
    OtherName            string         `gorm:"column:other_name;type:varchar(1024);not null" json:"other_name"`
    Gender               uint32         `gorm:"column:gender;type:tinyint unsigned;not null;index:idx_gender,priority:1;default:3;comment:1 男 2女 3 组合 4 unknown" json:"gender"` // 1 男 2女 3 组合 4 unknown
    Genres               string         `gorm:"column:genres;type:varchar(1024);not null;comment:genres" json:"genres"`                                                         // genres
    Popularity           int32          `gorm:"column:popularity;type:int;not null;comment:popularity" json:"popularity"`                                                       // popularity
    Notes                string         `gorm:"column:notes;type:varchar(255);not null;comment:备注,预留" json:"notes"`                                                             // 备注,预留
    SongMaterialQuantity int32          `gorm:"column:song_material_quantity;type:int;not null;comment:专辑内物料个数" json:"song_material_quantity"`                                  // 专辑内物料个数
}

type SongArtist struct {
    ID        string         `gorm:"column:id;type:varchar(64);primaryKey" json:"id"`
    CreateAt  time.Time      `gorm:"column:create_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);comment:创建时间" json:"create_at"` // 创建时间
    UpdateAt  time.Time      `gorm:"column:update_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);comment:修改时间" json:"update_at"` // 修改时间
    DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);comment:删除时间" json:"deleted_at"`                                     // 删除时间
    SongID    string         `gorm:"column:song_id;type:varchar(256);not null;uniqueIndex:uk_song_artist,priority:1;index:idx_song_id,priority:1" json:"song_id"`
    ArtistID  string         `gorm:"column:artist_id;type:varchar(256);not null;uniqueIndex:uk_song_artist,priority:2;index:idx_artist_id,priority:1" json:"artist_id"`
}
  • 使用join关联查询数据时候发现关联表没有自动过滤掉软删除的数据
  • 使用的sql
    err = gormDB.Debug().Model(&model.Artist{}).
        Select("artist.name").
        Joins("left join song_artist on artist.id = song_artist.artist_id").
        Where("song_artist.song_id = ?", songID).
        Scan(artistsNameArr).Error
  • 输出的sql
 SELECT artist.name FROM `artist` left join song_artist on artist.id = song_artist.artist_id WHERE song_artist.song_id = '003dc512121c4dfb898184565b92e8e8' AND `artist`.`deleted_at` IS NULL
  • 期望的sql
SELECT artist.name FROM `artist` left join song_artist on artist.id = song_artist.artist_id WHERE song_artist.song_id = '003dc512121c4dfb898184565b92e8e8' AND song_artist.deleted_at is null AND `artist`.`deleted_at` IS NULL

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