dongliao8069 2018-11-26 21:17 采纳率: 0%
浏览 93

gorm中的外键

here is my code :

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
)

type Mongo struct {
    gorm.Model
    Url      string
    Login    string
    Password string
    Env     string
    Base string
    Port string
}



type MicroService struct {
    gorm.Model
    Url        string
    Port       string
    Version    string
    Name       string
    Etat       string
    MongoDb    Mongo `gorm:"foreignkey:MongoId"`
    MongoId int
    Env     string
}



func initDb() {

    var mongoDb Mongo
    result := db.Find(&mongoDb, "env = ?", "Prod").RecordNotFound()
    fmt.Println("not found create mongodb")
    fmt.Println(result)
    if result == true {
        mongoDb = Mongo{}
        // "mongodb://heroku_4n1snp42:f42hkq6n3qfv48uelms7vrclta@:23930/heroku_4n1snp42"
        mongoDb.Url = "mlab.com"
        mongoDb.Login = "toto"
        mongoDb.Password = "titi"
        mongoDb.Base = "tata"
        mongoDb.Port = "23930"
        mongoDb.Env = "Prod"
        db.Create(&mongoDb)

    }
    var microService MicroService
    result = db.Find(&microService, "name = ? and env = ?", "constructProduct","Prod").RecordNotFound()
    fmt.Println("not found create construct Product Micro")
    fmt.Println(result)
    if result == true {
        microService = MicroService{}
        microService.Name  = "totobis"
        microService.Port = "8307"
        microService.Env = "Prod"
        var mongoDbConstructProduct Mongo
        result = db.Find(&mongoDbConstructProduct , "env = ?", "Prod").RecordNotFound()
        fmt.Println(mongoDbConstructProduct)
        microService.MongoDb = mongoDbConstructProduct
        db.Create(&microService)
        fmt.Println("constructProduct microservice created")
    }

}

var db *gorm.DB
var loggued bool





func main() {
    var err error
    loggued = false
    db, err = gorm.Open("sqlite3", "configuration.sqlite3?cache=shared&mode=rwc")
    db.AutoMigrate(&MicroService{})
    db.AutoMigrate(&Mongo{})

    initDb()
    var microService MicroService
    db.Find(&microService, "name = ?", "totobis").RecordNotFound()
    if err != nil {
        panic("failed to connect database")
    }
    defer db.Close()
    fmt.Println(microService)

}

But i don't understand why when i print the object after query it , the mongodb of MicroService is empty.

When i print the field mongodb in the creation it's ok :

{{1 2018-11-26 22:08:16.436503737 +0100 +0100 2018-11-26 22:08:16.504275897 +0100 +0100 } mlab.com toto titi Prod tata 23930}

But when i try to recreate a microservice object the mongo field is empty :

{{2 2018-11-26 22:09:15.73203579 +0100 +0100 2018-11-26 22:09:15.73203579 +0100 +0100 } 8307 totobis {{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC } } 1 Prod}

How can I do to get the good microservice struct with the mongo struct I've associated?

  • 写回答

1条回答 默认 最新

  • duancu4731 2018-11-26 22:22
    关注

    MongoId is a uint data type, not int.

    in another way you can use an int field in Mongo model as :

    type Mongo struct {
    gorm.Model
    Ms int // for example
    ....
    }
    

    and then in MicroService model :

    type MicroService struct {
    gorm.Model
    Url        string
    Port       string
    Version    string
    Name       string
    Etat       string
    MongoDb    Mongo `gorm:"foreignkey:MongoId;association_foreignkey:Ms"`
    MongoId int
    Env     string
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启