账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    go gorm select 了字段后 结果集还是所有的struct都被返回,只是其他字段为空
    50
    0

    go gorm select 了字段后 结果集还是所有的struct都被返回,只是其他字段为空

    问题出现的环境背景及自己尝试过哪些方法

    相关代码

    db.Debug().Where(s).Select([]string{"id","username","phone"}).Find(&user)

    type User struct {

    gorm.Model
    Username string `json:"username"`
    Phone string `json:"phone"`
    Type int8 `json:"type"`
    Order []Order `gorm:"ForeignKey:UId"`   // hasMany 设置对应的外键
    CreditCard *CreditCard `gorm:"foreignkey:CardID"`
    CardID uint

    }

    实际结果

    [
    {
    "id": 3,
    "created_at": "0001-01-01T00:00:00Z",
    "updated_at": "0001-01-01T00:00:00Z",
    "deleted_at": null,
    "username": "hello",
    "phone": "18672858778",
    "type": 0,
    "Order": null,
    "CreditCard": null,
    "CardID": 0
    },
    {
    "id": 6,
    "created_at": "0001-01-01T00:00:00Z",
    "updated_at": "0001-01-01T00:00:00Z",
    "deleted_at": null,
    "username": "hello",
    "phone": "18672858778",
    "type": 0,
    "Order": null,
    "CreditCard": null,
    "CardID": 0
    },
    {
    "id": 9,
    "created_at": "0001-01-01T00:00:00Z",
    "updated_at": "0001-01-01T00:00:00Z",
    "deleted_at": null,
    "username": "hello",
    "phone": "18672858779",
    "type": 0,
    "Order": null,
    "CreditCard": null,
    "CardID": 0
    },
    {
    "id": 12,
    "created_at": "0001-01-01T00:00:00Z",
    "updated_at": "0001-01-01T00:00:00Z",
    "deleted_at": null,
    "username": "hello",
    "phone": "18672858779",
    "type": 0,
    "Order": null,
    "CreditCard": null,
    "CardID": 0
    }
    ]

    期望结果

    [{
    "id":6,
    "username":"hello",
    "phone":"18672858779"
    },
    {
    "id":9,
    "username":"hello",
    "phone":"18672858779"
    }
    ]
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部