How to do a many-to-many query - go

I'm trying to understand how to use GORM to make query on items with many2many relations but I'm really lost.
I've got the following database model:
type Asset struct {
gorm.Model
Id uint `gorm:"primaryKey"`
MachineUID string `gorm:"type:varchar(128)" json:"machine_uid"`
AssetToken string `gorm:"uniqueIndex;type:varchar(128)"`
CommandQueries []*CommandQuery `gorm:"many2many:command_asset;"`
}
type CommandQuery struct {
gorm.Model
Id uint `gorm:"primaryKey"`
UUID string `gorm:"type:varchar(128)" json:"uuid"`
CmdType int `json:"cmdtype"`
CmdArgs string `gorm:"type:varchar(128)" json:"cmdargs"`
Assets *[]Asset `gorm:"many2many:command_asset;"`
Active bool
}
First, i'm successfully trying to retrieve an asset from a token with something like this:
token := "test-token"
var result Asset
db.Where("asset_token = ?", token).First(&result)
if result.Id == 0 {
return fmt.Errorf("Asset cannot be found in database")
}
But fom this returned struct, i would like to retrive all CommandQuery objects where:
this asset is in CommandQuery.assets
Where CommandQuery.active = true
I tried many things but nothing works, any help would be appreciated.

If I understood correctly, you want to load a slice of CommandQuery objects, and these objects should contain only assets where asset_token should be equal to the token you passed. Also, return only CommanQuery objects that have active=true.
If this is the case, it can be done like this:
token := "test-token"
var list []CommandQuery{}
tx := db.Preload("Assets", func (gdb *gorm.DB) *gorm.DB{
return gdb.Where("asset_token = ?", token)
}).
Where("active = ?", true).Find(&list)
if tx.Error == nil {
return nil, tx.Error
}
return *list, nil
In short, custom preloading is used to load assets into command query objects.

Related

Data binding in go using sqlBoiler

I am fetching some data from MYSQL database.. Using query data is getting correclty (eg 10 rows)
I want to bind into a list of model for displaying.
But panic error displaying
type UserDetails []UserDetail
type UserDetail struct {
id string `json:"id" boil:",bind"`
ScreenName string `json:"screenName" boil:",bind" `
}
func (m *mysqlStore) GetUsersDetails(ctx context.Context) () {
var userDetails []*models.UserDetail
err := queries.Raw(`
SELECT
user.id,
user.screen_name
FROM user
group by user.id
`).Bind(ctx, m.db, &userDetails)
if err != nil {
fmt.Println(err)
}
fmt.Println(userDetails)
}
here using the MYSQLQuery i am getting the correct data. I want to display that in a list of arrary eg:
[
{"id":"1",
"screenName":"test"},
{"id":"2",
"screenName":"test"}
]
what is the issue in my go code?
I got the Answer
In this case struct must be
type UserDetail struct {
id string `json:"id"`
ScreenName string `json:"screenName"`
}
and
var userDetails []models.UserDetail

One-to-many association

I'm having troubles with one-to-many associations in GORM.
I have those two structures and I'd like to get one patient's full history. Here is my sample code:
type Patient struct {
gorm.Model
Prenom string `json:"prenom" gorm:"column:patient_prenom"`
Nom string `json:"nom" gorm:"column:patient_nom"`
Genre string `json:"genre" gorm:"column:patient_genre"`
Naissance string `json:"naissance" gorm:"column:patient_naissance"`
Historique []Historique `gorm:"ForeignKey:Fk_patient_id"`
}
type Historique struct {
Fk_patient_id string
Date_consultation string
Fk_maladie_id uint
Fk_compte_medecin_id uint
Patient Patient
}
func GetPatientWithDiseases(id uint) (*Patient, error) {
patient := &Patient{}
//The line right there works so i can retrieve without the history
//err := GetDB().Find(patient, id).Error
db := GetDB().Preload("tt_historique").Find(patient)
err := db.Error
if err != nil {
return nil, err
}
return patient, nil
}
Where "Historique" uses the foreign key of the patient (Fk_patient_id), and the Historique []Historique is the list of every Historique that should end up in the Patient struct after the query.
However I get this error can't preload field tt_historique for models.Patient. I've tried multiple syntaxes that I've found on Internet in the gorm specifications in the struct but nothing worked. I've only been developing using GO for 3 days and GORM is my first ORM, so maybe I'm missing something really obvious.
Based on the presumption that tt_historique is your table name, there are a couple of things you need to take care of here.
By convention, go-gorm uses pluralized snake case struct names as database tables when constructing a SQL query. In your case, to preload the Historique []Historique field, it would look for the historiques table.
To override this, you need to implement the Tabler interface:
type Patient struct {
gorm.Model
Prenom string `json:"prenom" gorm:"column:patient_prenom"`
Nom string `json:"nom" gorm:"column:patient_nom"`
Genre string `json:"genre" gorm:"column:patient_genre"`
Naissance string `json:"naissance" gorm:"column:patient_naissance"`
Historique []Historique `gorm:"foreignKey:Fk_patient_id"`
}
type Historique struct {
Fk_patient_id string
Date_consultation string
Fk_maladie_id uint
Fk_compte_medecin_id uint
Patient Patient
}
func (Historique) TableName() string {
return "tt_historique"
}
Then, your query would look like this:
db := GetDB().Preload("Historique").Find(patient)

Gorm query returning only a single row

We're trying to use Gorm with mysql 8 to much frustration.
I have the following tables (simplified for brevity here)
type StoragePool struct {
gorm.Model
PoolId string `json:"id" gorm:"column:poolid;size:40;unique;not null"`
Volumes []Volume `json:"volumes" gorm:"foreignkey:StorageId;association_foreignkey:PoolId"`
}
type Volume struct {
gorm.Model
StorageId string `json:"storageid" gorm:"column:storageid;size:40"`
}
Data insertions seem to work fine. Both tables get populated and no constraints are violated.
A query that expects a single record seems to work fine:
poolRecord := &StoragePool{}
if err := tx.Where("poolid = ?", pool.PoolId).First(&StoragePool{}).Scan(poolRecord).Error; err != nil {
return err
}
This query only returns a single row. When I perform this exact query as raw SQL outside of go, it returns all 31 records I expect.
var poolVolumes []Volume
if err := tx.Where("storageid = ?", pool.PoolId).Find(&Volume{}).Scan(&poolVolumes).Error; err != nil {
return err
}
log.Debugf("found %d volumes belonging to %q [%s]", len(poolVolumes), pool.Name, pool.PoolId)
According to the docs, that second sql statement is the equivalent of "SELECT * FROM VOLUMES WHERE STORAGEID = 'poolid'". That is not the behavior I am getting.
Anyone have any ideas what I'm doing wrong here?
I rarely use an ORM while coding with go, but following the doc from gorm, it seems like you are doing it the wrong way.
Scan is used for scanning result into another struct, like this:
type Result struct {
Name string
Age int
}
var result Result
db.Table("users").Select("name, age").Where("name = ?", 3).Scan(&result)
The correct way to get query results into a slice of structs should be:
var poolVolumes []Volume
if err := tx.Where("storageid = ?", pool.PoolId).Find(&poolVolumes).Error; err != nil {
return err
}

Idiomatic way to update with GORM and echo-framework

Hi new in golang and trying to make rest API
I want to updating entity passing only needed datas with GORM and golang echo framework.
And I get this error :
"strconv.ParseUint: parsing \"ea78944c-a2a9-4813-86e7-10b199d0f002\": invalid syntax"
I use echo.Bind() func to bind formdata (I use postman) with my Club struct.
PS : I use xid to external id and keep my int id to interne work.
Expected :
I'm gonna find Club in my database by id (get with url param) with GORM and set it up in a new club struct.
After that, I bind my club struct with my formdata.
And finally save it with Save() GORM func. => get error
Reality :
I'm gonna find Club in my database by id (write hardcoding string id) with GORM and set it up in a new club struct.
After that, I bind my club struct with my formdata.
And finally save it with Save() GORM func. => works
Conclusion :
I don't know how to pass url parameter AND formdata in PUT method using echo.Bind()
Here my Club struct :
// Club struct
type Club struct {
Base
Name string `json:"name;" gorm:"type:varchar(255);not null" validate:"required"`
Slug string `json:"slug;" gorm:"type:varchar(255);unique;not null"`
Website string `json:"website;" gorm:"type:varchar(255)"`
Lat float32 `json:"lat;" gorm:"lat;" sql:"type:decimal(8,6);" validate:"required,numeric"`
Lng float32 `json:"lng;" gorm:"lng;" sql:"type:decimal(9,6);" validate:"required,numeric"`
Logo string `json:"logo;" gorm:"type:varchar(100)" validate:"required"`
Phone string `json:"phone;" gorm:"type:varchar(20)" validate:"required"`
}
My FindById func :
// GetClubByXID get club by xid
func GetClubByXID(c echo.Context, xid string) (*schemas.Club, error) {
club := new(schemas.Club)
if result := db.Where("xid = ?", xid).First(&club); result.Error != nil {
return nil, result.Error
}
return club, nil
}
And here my updating func :
func UpdateClub(c echo.Context) error {
xid := c.Param("id") // => doesn't work
// xid := "ea78944c-a2a9-4813-86e7-10b199d0f002" // => work
club, err := models.GetClubByXID(c, xid)
if err != nil {
return c.JSON(http.StatusNotFound, err)
}
if err := c.Bind(club); err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
db.Save(&club)
return c.JSON(http.StatusOK, echo.Map{
"club": &club,
})
}
My updating route :
API.PUT("/updateClub/:id", handlers.UpdateClub) // => doesn't work
// API.PUT("/updateClub", handlers.UpdateClub) // => work
When I write with hardcoding my xid ea78944c-a2a9-4813-86e7-10b199d0f002 in my updating func it's work like a charm but I can't combine my url and my formdata with echo.Bind()
my err if I try http://localhost:1323/api/updateClub/ea78944c-a2a9-4813-86e7-10b199d0f002:
"strconv.ParseUint: parsing \"ea78944c-a2a9-4813-86e7-10b199d0f002\": invalid syntax"
Thanks for reading hope someone can help me :)
Can you try to c.Param("xid") instead of c.Param("id") and change the routing parameter as API.PUT("/updateClub/:xid", handlers.UpdateClub)

Preload can't find field fieldName in *models.Catalog

I can't find an answer to my question. I am using jinzhu/gorm in a golang project:)
I have the following structs:
type Catalog struct {
ID int64 `gorm:"primary_key" form:"id"`
SubDomainID int64 `form:"sub_domain_id"`
ServiceTypeID int64 `form:"service_type_id"`
Checked bool `form:"checked"`
CreatedAt time.Time `form:"created_at"`
UpdatedAt time.Time `form:"updated_at"`
SubDomain SubDomain
}
type SubDomain struct {
Id int64 `gorm:"primary_key" form:"id"`
NameRu string `form:name_ru`
url string `form:url`
}
When I try to get catalog with preloading of subdomain:
var catalog Catalog
fmt.Println(catalog.SubDomain)
err := db.Preload("SubDomain").Where("checked = 0").First(&catalog).Error
if err != nil {
return &catalog, err
}
I get the following error: can't find field SubDomain in *models.Catalog
Why is this happening?
I expect there will be 2 queries:
select * from catalogs where checked = 0;
select * from sub_domains where id = (catalog.sub_domain_id)
I'm still new to gorm, but I think I know your problem and also have a (partial) solution.
As stated before (and you said so yourself), when applying "select * from..." it also looks for the field SubDomain (because it is in your struct).
So I believe this should work:
var catalog Catalog
fmt.Println(catalog.SubDomain)
err := db.Preload("SubDomain").Select("ID","SubDomainID", "ServiceTypeID").Where("checked = 0").First(&catalog).Error
if err != nil {
return &catalog, err
}
note how i specified the exact fields. A better solution will be to write a function to exclude member which are fields, using reflection. I am using a similar solution myself. it kinda looks like this:
for each member of Domain:
if member is string or boolean
fields.append(member).
return db.Select(fields) // actual gormdb's Select

Resources