go-gorm

go-gorm how to express many2many with additional columns

陌路散爱 提交于 2020-01-14 13:32:07
问题 I want to express the following tables in GORM: CREATE TABLE indexes ( id INTEGER PRIMARY KEY, name VARCHAR ) CREATE TABLE services ( id INTEGER PRIMARY KEY, name VARCHAR ) CREATE TABLE index_service ( index_id INTEGER REFERENCES indexes(id), service_id INTEGER REFERENCES services(id), write_active INTEGER, PRIMARY KEY (index_id, service_id) ) After reading through documentations and questions on stack overflow. I still cannot find an answer on how to express the additional column write

gorm golang one2many same table

孤人 提交于 2020-01-11 11:31:50
问题 I'm trying to create a self-reference in a (my)sql table using golang gorm. At the moment my code looks like this: type Person struct { gorm.Model Name string Children []*Person `gorm:"ForeignKey:ParentID"` ParentID uint } func main() { /* code to get database connection omitted */ p := &Person{Name:"Sally"} db.Create(p) children := []*Person{ {Name:"Jane", ParentID:p.ID}, {Name:"Tom", ParentID:p.ID}} for _, child := range children { db.Create(child) } var children2 []*Person db.Model(p)

How to get Description of MySQL Table in GoLang

。_饼干妹妹 提交于 2019-12-31 05:22:13
问题 com/jinzhu/gorm" and "github.com/go-sql-driver/mysql" package to interact with my database and trying to get the description of table but didn't found the function. Please help 回答1: With gorm you can perform a custom query and get her return in a struct , the following is an example of how to show the description of table: type Result struct { Field string Type string Null string Key string Default string Extra string } db.Raw("DESCRIBE TABLE_NAME").Scan(&result) View more by gorm: http:/

How to make connection between Docker Containers

有些话、适合烂在心里 提交于 2019-12-24 23:09:06
问题 I am having connection issues when I try to connect my goLang GORM service to a Docker Postgress container. I believe the problem is my golang code at the bottom at the connection string. docker-compose up Recreating postgress_postgre_1 ... done Attaching to postgres postgres | 2018-12-11 21:08:48.283 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 postgres | 2018-12-11 21:08:48.283 UTC [1] LOG: listening on IPv6 address "::", port 5432 postgres | 2018-12-11 21:08:48.291 UTC [1]

What is the most performant way to rewrite a large IN clause?

感情迁移 提交于 2019-12-23 20:07:45
问题 I wrote an API using go and gorm that runs calculations on our database and returns the results. I just hit the parameter limit for an IN condition when using an aggregate. Example query: SELECT SUM(total_amount) from Table where user_id in(...70k parameters) group by user_id One of my current edge cases has > 65535 user ids so my Postgres client is throwing an error: got 66037 parameters but PostgreSQL only supports 65535 parameters I'm not sure what the best way to approach this is. One

Get nested object in structure in gorm

送分小仙女□ 提交于 2019-12-23 15:25:27
问题 I have a two structure: type GoogleAccount struct { Id uint64 Token string } It represent my custom PostgreSQL object type (i created myself): CREATE TYPE GOOGLE_ACCOUNT AS ( id NUMERIC, token TEXT ); And next structure is table in DB: type Client struct { IdClient uint64 `gorm:"primary_key"` Name string PhotoUrl string ApprovalNumber uint16 Phone string Password string HoursOfNotice int8 Google GoogleAccount } And my custom object nested in type Client and named as google . I've tried to

how to import gorm db connection from another file or package

那年仲夏 提交于 2019-12-23 02:52:07
问题 I'm learning go and I've recently learned how to leverage gorm to connect to a database. I can't figure out how to import said connection. Only open and defer it's closing in the scope of the func main() What I have currently: func main(){ db, _ := gorm.Open("postgres", "host=localhost port=5432 user=someUser dbname=someDB password=somePW sslmode=disable") defer db.Close() } This works fine and I can create tables and do CRUD ... but all in the main function. Is there anyway I can do

How to create singleton DB class in GoLang

不打扰是莪最后的温柔 提交于 2019-12-21 04:12:32
问题 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

How to create a table with slices in columns

穿精又带淫゛_ 提交于 2019-12-20 07:45:25
问题 I have a model that looks like this: type Inventory struct { gorm.Model LocationID string Items []Item //this is a slice of structs Categories []Category //this is a slice of structs } When I create a table for it using gorm, I don't have the columns for Items or Categories. What am i missing? 回答1: Since arrays are not supported column types in SQL—most versions of SQL at least—gorm will not create columns for fields of type slice. You can, however, create the relationship structure you are

Selecting related fields with GORM

廉价感情. 提交于 2019-12-19 19:43:04
问题 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{},