草庐IT

go - 使用 gorm 模型的嵌套结构

我有一个名为User的结构:typeUserstruct{EmailstringNamestring}和名为UserDALModel的结构:typeUserDALModelstruct{gorm.Modelmodels.User}gorm模型看起来像这样:typeModelstruct{IDuint`gorm:"primary_key"`CreatedAttime.TimeUpdatedAttime.TimeDeletedAt*time.Time`sql:"index"`}这可以使UserDALModel嵌套在gorm模型和用户模型中,因此输出将是:{IDCreatedAtUpdate

go - 使用 Gorm 将值从一列映射到单独表中的另一列

鉴于以下one-to-many关系(一个Receipt有很多LineItem的),我想映射Price来自Receipt的字段表进入Price领域LineItem表(对于LineItem中的每个Product)。收据架构typeProductstruct{IDuint`json:"id"`TotalPricefloat64`json:"total"`LineItems[]LineItem`json:"lineItems"`}LineItem架构typeLineItemstruct{IDuint`json:"id"`ProductIDuint`json:"productID"`Pricef

postgresql - 使用 GoLang 保存额外数据

所以我使用GoLang1.10.3和Echo框架,以Gorm和Postgres作为我的数据库。我有三个表/结构,Profile、Addresses和Profile_addresses结构如下,简介typeProfilestruct{gorm.ModelCompanystring`gorm:"size:255"json:"company"`AddedDatetime.Time`gorm:"type:date"json:"date_added"`ProfileAddresses[]ProfileAddress`gorm:"foreignkey:profile_fk"json:"addres

有 OR 查询的 gorm

我一直在生成一个在运行时动态创建的查询。我想创建一个having查询,中间带有OR,例如SELECTnameFROM`user_group`WHERE((group_key='age'ANDgroup_value='20'))OR((group_key='division'ANDgroup_value='accounting'))OR((group_key='age'ANDgroup_value='22'))OR((group_key='division'ANDgroup_value='kitchen'))GROUP_BYnameHAVING((SUM(group_key='age'A

database - 在 go http 中的自定义处理程序中传递 *gorm.db 实例的最佳实践

我正在创建一个自定义网络处理程序来处理网络应用程序中的路由。处理程序是typeCustomHandlerstruct{Db*gorm.DB}然后是接收函数:func(hCustomHandler)Index()http.Handler{returnhttp.handlerFunc(whttp.ResponseWriter,r*http.Request){//Somecode//useh.Db.Find(),etc.})我将其传递给我的路由器作为//InpackagecustomHandler:=&CustomHandler{*gormInstance}//I'vealreadygott

go - gorm 中的外键

这是我的代码:packagemainimport("fmt""github.com/jinzhu/gorm"_"github.com/jinzhu/gorm/dialects/sqlite")typeMongostruct{gorm.ModelUrlstringLoginstringPasswordstringEnvstringBasestringPortstring}typeMicroServicestruct{gorm.ModelUrlstringPortstringVersionstringNamestringEtatstringMongoDbMongo`gorm:"foreig

go - 如何为实体 A 设置结构标签可能在 GORM 中有 B 并使创建和预加载自动工作

假设我有BaseNotification和ExtendedNotification结构。对于某些通知,他们仅使用base_notificaion表,但其中一些通知会将一些额外数据保存到extended_notificaion中。如何在这两个结构中设置结构标签,以便在我运行时db.Create(&entendedNotification)//orsomethingelse如果需要,GORM会知道它需要在extended_notification中插入一个新的元组(例如,它不是nil或字段已填充)当我调用db.Preload(`ExtendedNotification`).Find(&no

mysql - 使用 Gorm 更新为 0 值

我正在尝试使用gorm库更新一些值,但是没有更新值为0的字节和整数vartreatmentmodel.TreatmentDBerr=json.Unmarshal(b,&treatment)iferr!=nil{http.Error(w,err.Error(),500)return}fmt.Println(&treatment)db:=db.DB.Table("treatment").Where("id=?",nID).Updates(&treatment)此打印值是{0310002018-01-014001-01-01}那些0是字节值(数据库中的tinyint(1),如果我更改为int

Gorm : How do I set an integer column to null, 并更新内存中的模型?

作为一个简单的背景,我有一个表foo,带有一个可为空的int外键bar_id。我有一个函数可以从foo中删除bar关联,也就是将其设置为NULL。我已经尝试了一切:我尝试使用sql.NullInt64作为列类型,然后foo.BarId.Valid=false//evensetInt64=0forgoodmeasuredb.Save(&foo)//withLogMode(true)bar_id未在UPDATE语句中更新我试过:db.Raw("UPDATEfooSETbar_id=NULLWHEREid=?",foo.ID).Row().Scan(&foo)谢天谢地,SQL是正确的,但是对

sql-server - sql : Scan error on column index 0, name "": unsupported Scan, 将 driver.Value 类型 int64 存储到类型 *main.SMSBlast 中?

我现在正在尝试restfulapi,其中列SequenceID不是自动增量,因为故意的,当我像这样计数时,我的问题是库gormcountSequenceId:=db.Debug().Table("SMSBlast2").Count(&smsblast1),结果是sql:列索引0上的扫描错误,名称“”:不支持的扫描,将driver.Value类型int64存储到类型*main.SMSBlastpackagemainimport("encoding/json""fmt""github.com/gorilla/mux""github.com/jinzhu/gorm"_"github.com/