douhui8025 2019-01-09 15:30
浏览 13

使用结构使Mysql表联接时出错

I am developing the Go code where I am making the tables join using GORM, following the structs I have defined for that:

type MpUserFriend struct{
    gorm.Model
    FriendKey string
    SenderId  uint
    RecieverId  uint  `gorm:"index"`
    Reciever MpUser `gorm:"ForeignKey:RecieverId"`
    Status string
}

type MpUser struct{
    gorm.Model
    FirstName string
    MiddleName string
    LastName string
    Email string  `validate:"required,email"`
    UserName string  `validate:"required"`
    UserType string
    Status int
}
And following the way I am getting the data from the database:
friend := DbModel.MpUserFriend{}
friends := []DbModel.MpUserFriend{}

DB.Debug().Where("(sender_id = ? and reciever_id != ?) or (sender_id != ? and reciever_id=?)",strconv.Itoa(int(userAuth.UserDetail.ID)),strconv.Itoa(int(userAuth.UserDetail.ID)),strconv.Itoa(int(userAuth.UserDetail.ID)),strconv.Itoa(int(userAuth.UserDetail.ID)) ).Find(&friend)
DB.Debug().Model(&friend).Related(&friend.Reciever,"reciever_id").Find(&friends)

and following the output, I am getting from the above code:

                [0] => stdClass Object
                    (
                        [ID] => 3
                        [CreatedAt] => 2019-01-09T01:31:49-08:00
                        [UpdatedAt] => 2019-01-09T01:31:49-08:00
                        [DeletedAt] => 
                        [FriendKey] => jFF4tzj5ZG9Vh3l8X6ek6s-JdTWz0_QcrKVYfWrzqgk=
                        [SenderId] => 7
                        [RecieverId] => 1
                        [Reciever] => stdClass Object
                            (
                                [ID] => 0
                                [CreatedAt] => 0001-01-01T00:00:00Z
                                [UpdatedAt] => 0001-01-01T00:00:00Z
                                [DeletedAt] => 
                                [FirstName] => 
                                [MiddleName] => 
                                [LastName] => 
                                [Email] => 
                                [UserName] => 
                                [Status] => 0
                            )

                        [Status] => 0
                    )

Actually, I am not receiving the related user's detail, please help where I am making the mistake

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程