go-gorm

A complex update in GORM

非 Y 不嫁゛ 提交于 2021-02-11 14:33:01
问题 I have a model in GORM with Postgres as database. The model is like this type Country struct { gorm.Model Name string Population int64 Regions []Region } type Region struct { gorm.Model Name string Cities []City CountryID uint `sql:"type:bigint REFERENCES countries(id) ON DELETE CASCADE" json:"-"` } type City struct { gorm.Model Name string Comment string RegionID uint `sql:"type:bigint REFERENCES regions(id) ON DELETE CASCADE" json:"-"` } When I create a new record from model, I invoke

Update method does not update zero value

蓝咒 提交于 2021-02-11 14:13:08
问题 Original Question When using the Update method in GORM the new data does not get saved. i.e. I want to set a bool from true to false , but it stays true even after the Update method. In the description of the method there is a warning: "WARNING when update with struct, GORM will not update fields that with zero value" Since I am using a struct to update and false is the zero value of bool , this seems expected behaviour, but I don't see any reason why to do so and how to overcome this. func

Update method does not update zero value

邮差的信 提交于 2021-02-11 14:08:01
问题 Original Question When using the Update method in GORM the new data does not get saved. i.e. I want to set a bool from true to false , but it stays true even after the Update method. In the description of the method there is a warning: "WARNING when update with struct, GORM will not update fields that with zero value" Since I am using a struct to update and false is the zero value of bool , this seems expected behaviour, but I don't see any reason why to do so and how to overcome this. func

How to construct subquery in the form of SELECT * FROM (<subquery>) ORDER BY column;?

限于喜欢 提交于 2021-02-10 14:21:15
问题 I am using gorm to interact with a postgres database. I'm trying to ORDER BY a query that uses DISTINCT ON and this question documents how it's not that easy to do that. So I need to end up with a query in the form of SELECT * FROM (<subquery>) ORDER BY column; At first glance it looks like I need to use db.QueryExpr() to turn the query I have into an expression and build another query around it. However it doesn't seem gorm has an easy way to directly specify the FROM clause. I tried using

How to populate and embedded array with gorm?

南笙酒味 提交于 2021-02-07 10:56:17
问题 I have 2 structs with data like this: type User struct { Pics Pic[] } type Pic struct { Id int UserId int64 } Although everytime I insert an User, Each of the pics are inserted on their table everytime I find the users, pics are not populated: var users []User db.Limit(pagesize).Where("updated_at > ?", date).Find(&users) Am I doing something wrong? 回答1: Your models (the structs) don't really make sense because User have a Pic array indicates a 'one to many' user to pics relationship however

How to preload child conditionally based on parent column?

时间秒杀一切 提交于 2021-01-29 13:28:45
问题 I'm able to preload all Guests related to Order using this syntax: Table(order). Preload("Guests"). Where("order.code = ?", orderCode). First(&order). Error Is it possible to to preload Guests based on a condition on a column in Order table? here is SQL for what I want to achieve: SELECT * FROM orders WHERE code = "xyz" SELECT * FROM guests WHERE (order_id IN (1)) AND (some_column_in_guest_tbl = some_column_in_order_tbl) Note: I'm aware of this Preload syntax (this doesn't take value from

Gorm - Has One relation with anonymous field

岁酱吖の 提交于 2021-01-28 19:32:40
问题 I use Golang and GORM. I have a User structure which has one Association . type User struct { ID int ... } type Association struct { ID int UserID int } I also have an AssoUser structure, which is composed of a anonymous field User , and has a pointer to Assocation . type AssoUser struct { User Asso *Association } When I run var assoUser AssoUser assoUser.Asso = &Association{ Name : "asso_name", ... } assoUser.Name = "user_name" ... // filling the struct db.Debug().Create(&assoUser) I expect

Custom Gorm preloading does not fetch data

我是研究僧i 提交于 2021-01-18 06:28:11
问题 I have a query that fetches rows from jobs table and its author (each job has an author ), but I want to select specific fields. type User struct { ID uint `gorm:"primarykey" json:"-"` UUID uuid.UUID `gorm:"type:uuid not null" json:"-"` Email string `gorm:"type:varchar(255); not null" json:"email"` Name string `gorm:"type:varchar(255); not null" json:"name"` AvatarURL string `gorm:"type:varchar(255); not null" json:"avatar_url"` Provider string `gorm:"type:varchar(255); not null" json:

GORM create record that might already exist

▼魔方 西西 提交于 2021-01-03 05:30:06
问题 I'm using gorm with postgres in my Go app. I want to create a new user in the database, but there is a good chance that that user will already exist. If so, I want to not do anything with the database, but I want know about it so I can tell the user. The good news is, that's already what gorm.Create(..) does. Trying to create a record with a duplicate unique key will return an error. There are two problems: I want better error messages. I want to write custom user-facing error messages that

How do you do UUID in Golangs Gorm?

不羁岁月 提交于 2020-12-02 06:11:32
问题 I have the following model... type User struct { ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"` FirstName string `form:"first_name" json:"first_name,omitempty"` LastName string `form:"last_name" json:"last_name,omitempty"` Password string `form:"password" json:"password" bindind:"required"` Email string `gorm:"type:varchar(110);unique_index" form:"email" json:"email,omitempty" binding:"required"` Location string `form:"location" json:"location,omitempty"` Avatar string