Foreign key in 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(µService, "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(µService)
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(µService, "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?
go go-gorm
add a comment |
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(µService, "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(µService)
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(µService, "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?
go go-gorm
add a comment |
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(µService, "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(µService)
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(µService, "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?
go go-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(µService, "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(µService)
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(µService, "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?
go go-gorm
go go-gorm
edited Dec 5 '18 at 11:28
Flimzy
38.7k106597
38.7k106597
asked Nov 26 '18 at 21:17
user462794user462794
21631950
21631950
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489209%2fforeign-key-in-gorm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Nov 26 '18 at 22:22
MehdiMehdi
14517
14517
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489209%2fforeign-key-in-gorm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown