go-gorm

How to create singleton DB class in GoLang

☆樱花仙子☆ 提交于 2019-12-03 13:31:45
EDITED Solved :How should i create singleton DBManager class in GoLang. I Referred few code sample of how to create go singleton but i wish to have methods in those and call them on their singleton reference. My Code is as follows package dbprovider import ( "github.com/jinzhu/gorm" _"github.com/jinzhu/gorm/dialects/sqlite" "rest/article" "log" ) type DBOperations interface { AddArticle(article *article.Article) } type DBManager struct { db *gorm.DB isInitialized bool } var dbManagerInstance = new() func GetDBManager() DBManager { return dbManagerInstance } func new() DBManager { localDbRef,

Gorm always return structs with nil values

断了今生、忘了曾经 提交于 2019-12-02 12:13:06
I'm building a Go web API with Gorm as the ORM for Postgresql databases in Amazon RDS. Problem is Gorm always gives back a slice of structs which values are all nil, although the database is already populated with data. The number of structs in the slice are proper depending on the LIMIT I gave. I've also tried to directly query the SQL using database/sql builtin package, manually insert the variable inside rows.Next() loop and it works with no problem. I've tried this with 3 different tables with 3 different structs (obviously) and the result are all the same. So I guess it's a problem in the

Selecting related fields with GORM

◇◆丶佛笑我妖孽 提交于 2019-12-01 17:47:06
I'm new to Golang coming from a python background so trying to understand the new and different concepts. I'm trying to create related fields and then select them from the database. Models: type Company struct { gorm.Model Name string } type CreditCard struct { gorm.Model Number int Company Company CompanyId uint } type User struct { gorm.Model Name string CreditCard CreditCard CreditCardID uint } Create tables and rows and select from db common.DB.AutoMigrate( &Company{}, &CreditCard{}, &User{}, ) user := User{ Name: "Alice", CreditCard: CreditCard{Number: 123456, Company: Company{Name: "Bank

Golang Join array interface

安稳与你 提交于 2019-11-30 09:14:53
问题 I try to create bulk insert. I use gorm github.com/jinzhu/gorm import ( "fmt" dB "github.com/edwinlab/api/repositories" ) func Update() error { tx := dB.GetWriteDB().Begin() sqlStr := "INSERT INTO city(code, name) VALUES (?, ?),(?, ?)" vals := []interface{}{} vals = append(vals, "XX1", "Jakarta") vals = append(vals, "XX2", "Bandung") tx.Exec(sqlStr, vals) tx.Commit() return nil } But I got an error: Error 1136: Column count doesn't match value count at row 1 becuse i return wrong query INSERT

Golang Join array interface

余生长醉 提交于 2019-11-29 13:56:01
I try to create bulk insert. I use gorm github.com/jinzhu/gorm import ( "fmt" dB "github.com/edwinlab/api/repositories" ) func Update() error { tx := dB.GetWriteDB().Begin() sqlStr := "INSERT INTO city(code, name) VALUES (?, ?),(?, ?)" vals := []interface{}{} vals = append(vals, "XX1", "Jakarta") vals = append(vals, "XX2", "Bandung") tx.Exec(sqlStr, vals) tx.Commit() return nil } But I got an error: Error 1136: Column count doesn't match value count at row 1 becuse i return wrong query INSERT INTO city(code, name) VALUES ('XX1','Jakarta','XX2','Bandung', %!v(MISSING)),(%!v(MISSING), %!v

Gorm Golang orm associations

本小妞迷上赌 提交于 2019-11-28 18:15:19
I'm using Go with the GORM ORM . I have the following structs. The relation is simple. One Town has multiple Places and one Place belongs to one Town. type Place struct { ID int Name string Town Town } type Town struct { ID int Name string } Now i want to query all places and get along with all their fields the info of the corresponding town. This is my code: db, _ := gorm.Open("sqlite3", "./data.db") defer db.Close() places := []Place{} db.Find(&places) fmt.Println(places) My sample database has this data: /* places table */ id name town_id 1 Place1 1 2 Place2 1 /* towns Table */ id name 1

Gorm Golang orm associations

走远了吗. 提交于 2019-11-27 11:10:33
问题 I'm using Go with the GORM ORM. I have the following structs. The relation is simple. One Town has multiple Places and one Place belongs to one Town. type Place struct { ID int Name string Town Town } type Town struct { ID int Name string } Now i want to query all places and get along with all their fields the info of the corresponding town. This is my code: db, _ := gorm.Open("sqlite3", "./data.db") defer db.Close() places := []Place{} db.Find(&places) fmt.Println(places) My sample database