dqyq88053 2018-10-10 17:54
浏览 110
已采纳

使用GoLang保存额外的数据

So I am using Go Lang 1.10.3 with the Echo framework with Gorm and Postgres as my database.

I have the three tables / structs , Profile, Addresses and Profile_addresses

The structs are as follows,

Profile

type Profile struct {
  gorm.Model
  Company string `gorm:"size:255" json:"company"`
  AddedDate time.Time `gorm:"type:date" json:"date_added"`
  ProfileAddresses []ProfileAddress `gorm:"foreignkey:profile_fk" json:"address_id"`
}

Address

type Address struct {
  gorm.Model 
  AddressLine1 string `gorm:"size:255" json:"line1"`
  AddressLine2 string `gorm:"size:255" json:"line2"`
  AddressLine3 string `gorm:"size:255" json:"line3"`
  City string `gorm:"size:200" json:"city"`
  Country string `gorm:"size:255" json:"country"`
  Postcode string `gorm:"size:12" json:"postcode"`
  ProfileAddresses []ProfileAddress `gorm:"foreignkey:address_fk"`
}

And Profile Address

type ProfileAddress struct {
  gorm.Model
  Archive bool `json:"archive"`

  Profile Profile
  Address Address
  AddressFK int`gorm:"ForeignKey:id"`
  ProfileFK int`gorm:"ForeignKey:id"`
}

These tables all get made fine but I am now trying to save an address ID to the Profile Address table when making a new Profile. Posting data to /profile/add (with Postman) including the following data

{
  "company": "testing-000001",
  "date_added": "3051-11-09T00:00:00+00:00",
  "address_id": 3
}

Now I can save a new profile and a new address but not save this data. I have only just added the json:"address_id"` option to the end of the Profile struct but that did not work.

I have set this up like so, as one profile might have many address so there needs to be a linked table with all the address ids. I sure that I could do this in a two stage step, e.g. save the profile, then save the addresses I want to add to that profile but I would like to get this to work. I also dont need to create a new address, these would have already been added into the system.

So what am I doing wrong?

Any help most welcome.

Thanks,

  • 写回答

1条回答 默认 最新

  • dou91736 2018-10-12 14:40
    关注

    This is how I got it to work, please let me know if this is not correct or if there is a better way.

    So I added the Profile struct to a wrapper/nested struct, like so:

    type ProfileWrapper struct {
      gorm.Model
      ProfileData Profile
      AddressID int `json:"address_id"`
    }
    

    So I then changed/updated the json data I was sending into this struct to this:

    {
      "ProfileData": {
          "company": "testing-with-add-0001",
          "date_added": "9067-11-09T00:00:00+00:00"
      },
      "address_id": 3
    }
    

    So to saved the profile I did this,

    db.Create( &p.ProfileData )
    

    I then built a new struct for the Profile Address information to be created/added, like so:

    pd := structs.ProfileAddress{AddressFK: p.AddressID, ProfileFK: profileID}
    
    db.Create( &pd )
    

    Not sure if this is the best way of doing this, but it seems to work. If this is wrong please let me know.

    Thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?