Skip to content

Preload many2many problem #688

Closed
Closed
@ukkii

Description

My structs

type BaseModel struct {
    ID        uint          `gorm:"primary_key" json:"id"`
    CreatedAt time.Time     `json:"-"`
    UpdatedAt time.Time     `json:"-"`
    DeletedAt *time.Time    `json:"-"`
}

type Show struct {
    BaseModel
    Name           string `json:"name""`
    Videos         []Video `gorm:"many2many:show_videos;"`
}

type Video struct {
    BaseModel
    StreamURL string `json:"streamURL"`
}

My code:

var shows []models.Show
dbMap.Preload("Videos").Order("name asc").Find(&shows)

SQL

SELECT  * FROM "shows"  WHERE ("shows".deleted_at IS NULL OR "shows".deleted_at <= '0001-01-02') ORDER BY name asc
SELECT  * FROM "videos" INNER JOIN show_videos ON show_videos."video_id" = "videos"."id" WHERE (show_id in ())

When table shows is empty, I get error:

ERROR:  syntax error at or near ")"
LINE 1: ...show_videos."video_id" = "videos"."id" WHERE (show_id in ())
                                                                     ^

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions