dongliang9682 2019-09-14 03:38
浏览 96

如何使协会的请求主体属于一个组织

I have People and Data , where People has one Data and Data belongs to People how to make a request body JSON for that Association in go gin? I am using gorm for this case, the documentation of gorm is not clear for me for this case, i was supposed like

func CreateTodo(db *gorm.DB) func(c *gin.Context) {
    var person Person
    var data Data 

    c.bindJSON(&Person)
    c.bindJSON(&Data)

    db.create(&Person)
    db.create(&Data)

    c.JSON(200, gin.H{ result : []interface{person, data})
}
type (
    Data struct {
        ID          uint `gorm:"auto_increment"`
        PersonID    uint
        Person      *Person `gorm:"foreignkey:PersonID;association_foreignkey:id"`
        Address     string
        Languange   string
    }

    Person struct {
        gorm.Model
        Email    string `gorm:"type:varchar(100);unique_index;not null"`
        Password string `gorm:"type:varchar(100);not null"`
        Role     string `gorm:"size:30;not null"`
        DataID   uint
        Data     *Data `gorm:""foreignkey:DataID;association_foreignkey:id"`
    }
)

I am sure it will not make the person_id and data_id for FK what I ask, how I can make the request body for that Association until those request created with FK itself ? should I create then update again for person_id and data_id after it created ??

  • 写回答

1条回答 默认 最新

  • douhuan6157 2019-09-14 04:29
    关注

    Gorm will do almost everything for an association link. It seems that "DataID" in your Person struct is useless. See the code below for an example:

    package main
    
    import (
        "github.com/jinzhu/gorm"
        _ "github.com/jinzhu/gorm/dialects/sqlite"
    )
    
    type (
        Data struct {
            ID        uint `gorm:"auto_increment"`
            PersonID  uint
            Person    *Person `gorm:"foreignkey:PersonID;association_foreignkey:id"`
            Address   string
            Languange string
        }
    
        Person struct {
            gorm.Model
            Email    string `gorm:"type:varchar(100);unique_index;not null"`
            Password string `gorm:"type:varchar(100);not null"`
            Role     string `gorm:"size:30;not null"`
            Data     *Data  `gorm:""foreignkey:PersonID;association_foreignkey:id"`
        }
    )
    
    func main() {
        db, err := gorm.Open("sqlite3", "test.db")
        if err != nil {
            panic("failed to connect database")
        }
        db.LogMode(true)
        defer db.Close()
    
        // Migrate the schema
        db.AutoMigrate(&Person{}, &Data{})
    
        data := &Data{
            Address:   "Shanghai,China",
            Languange: "Chinese",
        }
        person := &Person{
            Email: "zhjw43@163.com",
            Data:  data,
        }
        db.Save(person)
    
        db.DropTable("data", "people")
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示