如何访问gorm.Model.ID?--CSDN问答

关注

码龄 粉丝数 原力等级 --

  • 被采纳
  • 被点赞
  • 采纳率

drydaenth257216154

2018-07-26 08:26

浏览 418

首页/

已采纳

如何访问gorm.Model.ID?

So gorm.Model provides some base properties or fields:

ID        uint       `json:"-" gorm:"primary_key"`CreatedAt time.Time  `json:"-"`UpdatedAt time.Time  `json:"-"`DeletedAt *time.Time `json:"-" sql:"index"`

and you can use it as so

type User struct {  gorm.Model  Name         string  Email        string  `gorm:"type:varchar(100);unique_index"`  Role         string  `gorm:"size:255"` // set field size to 255 }

So when I was working on my Model Controller(s) for delete (or anything where I needed to compare the ID)

This does not work, give me an error:

c.Ctx.DB.Delete(&models.Address{ID: id})
unknown field 'ID' in struct literal of type github.com/NlaakStudios/PASIT/models".Address

And, this does not work, give me an error:

 c.Ctx.DB.Delete(&models.Address{gorm.Model.ID: id})
invalid field name gorm.Model.ID in struct initializer id int

If I remove the gorm.Model and define the field myself in each model ... it works.

type User struct {ID        uint       `json:"-" gorm:"primary_key"`CreatedAt time.Time  `json:"-"`UpdatedAt time.Time  `json:"-"`DeletedAt *time.Time `json:"-" sql:"index"`  Name         string  Email        string  `gorm:"type:varchar(100);unique_index"`  Role         string  `gorm:"size:255"` // set field size to 255}

How do I access those four base fields?

展开全部

  • 写回答
  • 好问题 0 提建议
  • 追加酬金
  • 关注问题
  • 微信扫一扫

    点击复制链接

    分享

  • 邀请回答
  • 编辑 收藏 删除 结题
  • 收藏 举报 取消加精
  • 收起

追加酬金 (90%的用户在追加酬金后获得了解决方案)

当前问题酬金

¥ 0 (可追加 ¥500)

¥ 10_¥_ 20_¥_ 50_¥_ 100 _¥_请输入10~500的整数

支付方式

余额支付

余额: ¥ 0

扫码支付

您当前余额不足,请更换为 扫码支付

提供问题酬金的用户不参与问题酬金结算和分配

支付即为同意 《付费问题酬金结算规则》

确认追加

删除 再等等

结题 再想想


原网址: 访问
创建于: 2022-09-15 20:58:38
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共1条评论
DeepMind

我是果粉我怕谁

I worked with this: removing package Name 'gorm'c.Ctx.DB.Delete(&models.Address{Model: gorm.Model{ID: id}})

2022-09-15 20:59:33 回复