go-gorm

how preload a full hierarchy in GO using GORM

余生长醉 提交于 2019-12-14 04:17:18
问题 I have a hierachy composed by several structures type Entry struct { Id int CreatedAt time.Time UpdatedAt time.Time Fields []Field } type SyncField struct { Id int CreatedAt time.Time UpdatedAt time.Time TechnicalName string JsonName string EntryId int Decorators []Decorator } type Decorator struct { Id int CreatedAt time.Time UpdatedAt time.Time Name string Description string SortingOrder int Params string SyncFieldId int } My DB creation is definded like this : db.CreateTable(&Entry{}) db

calling model dynamically in go

混江龙づ霸主 提交于 2019-12-14 03:04:46
问题 Consider products like jeans, shirts, shorts and I want to store the orders in respective product tables for eg jeans related order should get stored in jeans tables and so on. Every table would have identical parameters. So while storing orders in table I should be able to call the respective struct and store the order. I am coming from Laravel (PHP) background where I can load dynamic model like $model = "Dynamic passed model names" $class = "App\\Models\\$model"; but in Go how can we do

Filtering by date in GORM

。_饼干妹妹 提交于 2019-12-13 00:13:16
问题 I'm using GORM to access the records in my database. Now I want to retrieve all records that are not deleted which means, that the attribute DeletedAt must be NULL. I tried the following command chains with WHERE(), but they returned no results. users := []*models.User{} db.Where("deleted_at", nil).Find(&users) and db.Where("deleted_at", "NULL").Find(&users) My database model is defined by the following structs: type Model struct { ID uint `gorm:"primary_key"` CreatedAt time.Time UpdatedAt

gorm golang one2many same table

喜你入骨 提交于 2019-12-11 16:54:42
问题 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)

Gorm returns just one instead of multiple results

≯℡__Kan透↙ 提交于 2019-12-11 15:44:28
问题 I wrote blow codes and it returns just 1 row instead of 4: package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" ) type Post struct { gorm.Model Title string Text string Comments []Comment } type Comment struct { gorm.Model Text string PostID uint `gorm:"foreignkey:ID;association_foreignkey:PostID"` } func main() { db, err := gorm.Open("sqlite3", "test.db") if err != nil { panic("failed to connect to database") } defer db.Close() db.DropTableIfExists(

Postgres infinite self join

五迷三道 提交于 2019-12-11 15:43:39
问题 So i have an article, and "comments" on the article.. the comment allows people to reply.. and you could reply to the reply.. so on and so forth, meaning the deepest tree root would be N Quick mockup of what the tables look like Comments(id, news_id, user_id, body, likes) Replies(id, parent_id) --> id here is = Comments.id User(id, username, password) News(id, title, body, image) Is there a way to query the Postgres DB to give me a result of something like So anything inside the Replies table

define associative model in Golang gorm

我只是一个虾纸丫 提交于 2019-12-11 15:13:09
问题 I am using golang gorm in my RestFul service, however, now I have a doubt that might be simple but I cannot find any example or specific documentation, its not clear to me. Let's say that I have the tables users and languages, any user can have many languages and any language can have many users, in this case for theory of relational database modeling we have to create a table users_languages, and checking gorm I see that I will have to use many to many relationship. By now, I have the

Auto Preloading with gorm not working as expected

时间秒杀一切 提交于 2019-12-11 12:47:38
问题 I am trying to auto-preload my models but am having difficulty doing so. These are the models i am using : package domain type User struct { gorm.Model Name string Car Car `gorm:"auto_preload"` Account Account `gorm:"auto_preload"` } type Car struct { gorm.Model Type int UserID uint } type Account struct { gorm.Model Powerlevel int UserID uint } This is the code i am executing to test the auto-preloading feature: func main() { db, err := gorm.Open("sqlite3", "./data.db") if err != nil {

How to set isolation level

早过忘川 提交于 2019-12-11 12:40:42
问题 I want to set isolation level to repeatable read . How do I achieve this using gorm orm for postgres. Example code: func CreateAnimals(db *gorm.DB) err { tx := db.Begin() // Note the use of tx as the database handle once you are within a transaction if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { tx.Rollback() return err } if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil { tx.Rollback() return err } tx.Commit() return nil } 回答1: I had exactly the same problem

Golang database manager api concept, error with type assertion

倾然丶 夕夏残阳落幕 提交于 2019-12-11 06:48:34
问题 The base concept creating a Database Manager API for getting data through an API. I am using the GORM for getting data of the instances of the strcuts. So there is 300-400 struct which represents the tables. type Users struct { ID int64 Name string } type Categories struct { ID int64 Category string } The next step I implement a function which is return the correct instance of the struct by table name, what I get through the API endpoint param. func GetModel(model string) interface{} { switch