I am trying to get extra columns from a many2many relationships on Gorm. Example
Part
type Part struct {
Id unit
Name string
}
Diagram
type Diagram struct {
Id unit
Name string
Parts []Part `gorm:"many2many:diagram_parts;"`
}
DiagramPart
type DiagramPart struct{
DiagramId uint `gorm:"primaryKey"`
PartId uint `gorm:"primaryKey"`
PartDiagramNumber int
PartNumber string
PartDescription string
}
This is what I have done trying to retrieve PartNumber and PartDescription in Parts.
diagram := &Diagram{}
db := s.db.Where("id = ?", 1).
Preload("Parts", func(db *gorm.DB) *gorm.DB {
return db.Select("parts.*, diagram_parts.part_number, diagram_parts.part_description").
Joins("left join diagram_parts on diagram_parts.part_id = parts.id")
}).
First(diagram)
Unfortunately, I am not able to retrieve part_number, part_description. How should I go about it?
You can add field PartNumber and PartDescription on struct Part OR Diagram, then add tag gorm:"-:migration;->" on than fields to ignore migration and to readonly mode. But on your situation, you can add it in struct Part because you already preload it.
source: https://gorm.io/docs/models.html#Field-Level-Permission
here's the example:
package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Part struct {
Id uint `gorm:"primaryKey"`
Name string
PartNumber string `gorm:"-:migration;->"`
PartDescription string `gorm:"-:migration;->"`
}
type Diagram struct {
Id uint `gorm:"primaryKey"`
Name string
Parts []Part `gorm:"many2many:diagram_parts;"`
// PartNumber string `gorm:"-:migration;->"`
// PartDescription string `gorm:"-:migration;->"`
}
type DiagramPart struct {
DiagramId uint `gorm:"primaryKey"`
PartId uint `gorm:"primaryKey"`
PartDiagramNumber int
PartNumber string
PartDescription string
}
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Diagram{}, &Part{}, &DiagramPart{})
diagram := &Diagram{}
err = db.Debug().Where("id = ?", 1).
// Select("diagrams.*, diagram_parts.part_number, diagram_parts.part_description").
Preload("Parts", func(db *gorm.DB) *gorm.DB {
return db.Select("parts.*, diagram_parts.part_number, diagram_parts.part_description").
Joins("left join diagram_parts on diagram_parts.part_id = parts.id")
}).
// Joins("left join diagram_parts on diagram_parts.diagram_id = diagrams.id").
First(diagram).Error
if err != nil {
panic(err)
}
fmt.Printf("diagram: %v\n", diagram)
fmt.Println("part number:", diagram.Parts[0].PartNumber)
}
Related
I am using Gorm and DB driver PostgreSQL and i have struct like this :
Projects
type Project struct {
ID int
ListingOrigin *ListingOrigin `gorm:"foreignkey:ListingID"`
}
func (Project) TableName() string {
return "project.projects"
}
ListingOrigin
type ListingOrigin struct {
ID int
ListingID string `gorm:"not null"`
OriginID uint `gorm:"not null"`
Project *Project `gorm:"foreignKey:ListingID;references:id"`
}
func (l *ListingOrigin) TableName() string {
return "listing_origins"
}
As u can see we can do Preload using ID on table Project, but first, I need to convert that value to string or integer.
When I do like this :
func (r *ProjectRepository) Options() (result []model.Project, err error) {
var projects []model.Project
query := r.db.Model(projects).Preload("ListingOrigin")
query.Order("name asc")
err = query.Find(&projects).Error
if err != nil {
return nil, err
}
return projects, nil
}
the error occurred :
ERROR: operator does not exist: character varying = integer (SQLSTATE 42883)
[rows:0] SELECT * FROM "listing_origins" WHERE "listing_origins"."listing_id" IN (1,3,4) AND "listing_origins"."deleted_at" IS NULL
Is there any way to do this ?
I have a table that represents my user data. There is a field that represents a phone number, and I want to store the hash of that automatically in the database at either update or insert.
so my model is :
type Users struct {
gorm.Model
ID uint `gorm:"autoIncrement;unique" json:"id"`
PhoneNumber string `json:"phone_number"`
HashID string `gorm:"primaryKey" json:"hash_id"`
Name string `gorm:"default:dear user" json:"name"`
Rank uint `json:"rank"`
Score uint `json:"score"`
Image string `json:"image"`
Email string `json:"email"`
Address string `json:"address"`
Birthday string `json:"birthday"`
Biography string `json:"biography"
}
How can I tell the GORM to fill the HashID column with the sha256 hash code of the PhoneNumber column while inserting or updating data?
You need something like this:
package main
import (
"crypto/sha256"
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Users struct {
gorm.Model
Key string `json:"phone_number"`
Hash string `gorm:"primaryKey" json:"hash_id"`
}
func (u *Users) BeforeCreate(tx *gorm.DB) (err error) {
h := sha256.Sum256([]byte(u.Key))
u.Hash = fmt.Sprintf("%x", h[:])
return nil
}
func (u *Users) BeforeSave(tx *gorm.DB) (err error) {
h := sha256.Sum256([]byte(u.Key))
u.Hash = fmt.Sprintf("%x", h[:])
return nil
}
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Users{})
u := Users{Key: "123"}
db.Create(&u)
}
Check https://gorm.io/docs/index.html
We can Easy to get the child of hasMany relation in Golang Gorm with Preload.
But how to get reverse of relation.
type Owner struct {
ID int `gorm:"column:id" json:"id"`
Name string `gorm:"column:name" json:"name"`
Projects []Project `gorm:"foreignkey:OwnerID" json:"projects"`
}
type Project struct {
ID int `gorm:"column:id" json:"id"`
Name string `gorm:"column:name" json:"name"`
OwnerID int `gorm:"column:owner_id" json:"owner_id"`
Gallery []Gallery `gorm:"foreignkey:ProjectID" json:"gallery"`
}
type Gallery struct {
ID int `gorm:"column:id" json:"id"`
ProjectID int `gorm:"column:project_id" json:"project_id"`
Url string `gorm:"column:url" json:"url"`
Title string `gorm:"column:title" json:"title"`
Description string `gorm:"column:description" json:"description"`
}
we can populate Gallery inside Project with Preload, like this:
db.Preload("Gallery").Find(&project)
How to get reverse, we want load project from gallery or owner from project?
the result I want some thing like this, when get project in form of json:
{
"id": 1,
"name": "Name Of Project",
"owner": {
"id":1,
"name": "Owner 1"
},
"gallery":[]
}
I am a bit late to the game with this answer, but you can define inverse relationships (belongs to) on the child models by doing this:
type ParentModel struct {
Children []Child // This is a 'has many'
}
type Child struct {
ParentModelID int // These behave as a 'belongs to'
ParentModel ParentModel // You need both the model and the modelID
}
Full working example
This uses the models from your question. A note on performance, the preloading in gorm isn't done via joins. It's done via additional SQL queries, the more nesting the more queries. If you have commonly executed preloading, it could be worth writing a raw query and a customer scanner to use an optimised query.
package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Owner struct {
gorm.Model
Projects []Project
}
type Project struct {
gorm.Model
OwnerID int
Owner Owner
Gallery []Gallery
}
type Gallery struct {
gorm.Model
ProjectID int
Project Project
}
func main() {
db, err := gorm.Open(sqlite.Open("many2many.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
err = db.AutoMigrate(&Owner{}, &Project{}, &Gallery{})
if err != nil {
return
}
ownerOne := Owner{}
db.Create(&ownerOne)
projectOne := Project{Owner: ownerOne}
projectTwo := Project{Owner: ownerOne}
db.Create(&projectOne)
db.Create(&projectTwo)
galleryOne := Gallery{Project: projectOne}
galleryTwo := Gallery{Project: projectOne}
galleryThree := Gallery{Project: projectTwo}
galleryFour := Gallery{Project: projectTwo}
db.Create(&galleryOne)
db.Create(&galleryTwo)
db.Create(&galleryThree)
db.Create(&galleryFour)
// Find by project and preload owners
fetchedProject := Project{}
db.Preload("Owner").Find(&fetchedProject, projectOne.ID)
fmt.Println(fetchedProject.Owner)
// Find by a gallery and preload project and owner
fetchedGallery := Gallery{}
db.Preload("Project.Owner").Find(&fetchedGallery, galleryOne.ID)
fmt.Printf("Gallery.id = %d --> Project.id = %d --> Owner.id = %d\n", fetchedGallery.ID,
fetchedGallery.Project.ID, fetchedGallery.Project.Owner.ID)
}
I have struct called User:
type User struct {
Email string
Name string
}
and struct called UserDALModel:
type UserDALModel struct {
gorm.Model
models.User
}
gorm Model looks like this:
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
this is possible to make UserDALModel nested with gorm model and user model so the output will be:
{
ID
CreatedAt
UpdatedAt
DeletedAt
Email
Name
}
now the output is:
{
Model: {
ID
CreatedAt
UpdatedAt
DeletedAt
}
User: {
Name
Email
}
}
According to this test in gorm, I think you need to add an embedded tag to the struct.
type UserDALModel struct {
gorm.Model `gorm:"embedded"`
models.User `gorm:"embedded"`
}
You can also specify a prefix if you want with embedded_prefix.
I found the answer:
type UserModel struct {
Email string
Name string
}
type UserDALModel struct {
gorm.Model
*UserModal
}
------------------------------
user := UserModel{"name", "email#email.com"}
userDALModel := UserDALModel{}
userDal.UserModal = &user
be careful embedding two structs with the same column:
package tests
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"testing"
)
type A struct {
X string
Y string
}
type B struct {
X string
Y string
}
type AB struct {
B B `gorm:"embedded"` // Embedded struct B before struct A
A A `gorm:"embedded"`
}
var DB *gorm.DB
func connectDB() error {
var err error
spec := "slumberuser:password#tcp(localhost:3306)/slumber"
DB, err = gorm.Open("mysql", spec+"?parseTime=true&loc=UTC&charset=utf8")
DB.LogMode(true) // Print SQL statements
//defer DB.Close()
if err != nil {
return err
}
return nil
}
// cd tests
// go test -run TestGormEmbed
func TestGormEmbed(t *testing.T) {
if err := connectDB(); err != nil {
t.Errorf("error connecting to db %v", err)
}
values := []interface{}{&A{}, &B{}}
for _, value := range values {
DB.DropTable(value)
}
if err := DB.AutoMigrate(values...).Error; err != nil {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
}
DB.Save(&A{X: "AX1", Y: "AY1"})
DB.Save(&A{X: "AX2", Y: "AY2"})
DB.Save(&B{X: "BX1", Y: "BY1"})
DB.Save(&B{X: "BX2", Y: "BY2"})
//select * from `as` join `bs`;
// # x,y,x,y
// # AX1,AY1,BX1,BY1
// # AX2,AY2,BX1,BY1
// # AX1,AY1,BX2,BY2
// # AX2,AY2,BX2,BY2
var abs []AB
DB.Select("*").
Table("as").
Joins("join bs").
Find(&abs)
for _, ab := range abs {
fmt.Println(ab.A, ab.B)
}
// if it worked it should print
//{AX1 AY1} {BX1 BY1}
//{AX2 AY2} {BX1 BY1}
//{AX1 AY1} {BX2 BY2}
//{AX2 AY2} {BX2 BY2}
// but actually prints
//{BX1 BY1} {AX1 AY1}
//{BX1 BY1} {AX2 AY2}
//{BX2 BY2} {AX1 AY1}
//{BX2 BY2} {AX2 AY2}
}
I have a query with 2 Joins to other tables, the query executes and returns all the fields from the primary table but I am not sure how I can get access to the joined fields.
in func LastTest() I am setting the var result to the TestResultDetail table, I would also like access to TestResult and SeqTestStage values in my result which are joined in the query.
Post query I can access all of the result fields in the TestResult table but none of the tables I joined on.
TestResultDetail has a FK relation to TestResult.
There are many TestResultDetail records for a single TestResult record.
For example seq_test_stage.description or any field in seq_test_stage comes over as empty.
main.go
package main
import (
"./models"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
func main() {
router := gin.Default()
opt := router.Group("opt/v2")
{
opt.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
opt.GET("/last-completed-test/:serial", LastTest)
}
// Add API handlers here
router.Run(":3000")
}
func LastTest(c *gin.Context) {
// Connection to the database
db := models.InitDB()
defer db.Close()
var result models.TestResultDetail
type Response struct {
Status string
Id int64
Serial string
Product string
Stage string
}
// get param and query
serial := c.Params.ByName("serial")
db.Model(&models.TestResultDetail{}).
Select("test_result.id, test_result.serial, test_result.product, test_result_detail.stage_id, seq_test_stage.description").
Joins("join test_result on test_result.ID = test_result_detail.result_id").
Joins("join seq_test_stage on seq_test_stage.ID = test_result_detail.stage_id").
Where("test_result.serial = ?", serial).
Order("test_result_detail.id desc").
Limit(1).
Scan(&result)
if result.ID != 0 {
res1 := &Response{
Status: "OK",
Id: result.ResultId.ID,
Serial: result.ResultId.Serial,
Product: result.ResultId.Product,
Stage: result.StageId.Description,
}
c.JSON(200, res1)
} else {
// Display JSON error
c.JSON(404, gin.H{"error": "No Records", "code": 404})
}
}
test_result_detail.go
package models
import (
"time"
)
type TestResultDetail struct {
ID int64 `gorm:"primary_key" db:"id" json:"id"`
ResultId TestResult
StatusId ResultStatus
StationId Station
StageId SeqTestStage
OperatorId AuthUser
Failstep string `db:"fail_step" json:"fail_step"`
Shift string `db:"shift" json:"shift"`
SequenceRev int `db:"sequence_rev" json:"sequence_rev"`
DateAdded time.Time `db:"date_added" json:"date_added"`
DateTimestamp time.Time `db:"date_timestamp" json:"date_timestamp"`
DateTime time.Time `db:"date_time" json:"date_time"`
StageOrder int `db:"stage_order" json:"stage_order"`
SerialNumber string `db:"serial_number" json:"serial_number"`
IsRetest int `db:"is_retest" json:"is_retest"`
RetestReason string `db:"retest_reason" json:"retest_reason"`
}
test_result.go
package models
import (
"time"
)
type TestResult struct {
ID int64 `gorm:"primary_key" db:"id" json:"id"`
DateAdded time.Time `db:"date_added" json:"date_added"`
Serial string `db:"serial" json:"serial"`
SequenceId int `db:"sequence_id" json:"sequence_id"`
LastCompletedStage int `db:"last_completed_stage" json:"last_completed_stage"`
LastCompletedSequence int `db:"last_completed_sequence" json:"last_completed_sequence"`
Workorder string `db:"workorder" json:"workorder"`
Product string `db:"product" json:"product"`
IsComplete string `db:"is_complete" json:"is_complete"`
IsScrapped string `db:"is_scrapped" json:"is_scrapped"`
ValueStream string `db:"value_stream" json:"value_stream"`
PromiseDate string `db:"promise_date" json:"promise_date"`
FailLock int `db:"fail_lock" json:"fail_lock"`
SequenceRev int `db:"sequence_rev" json:"sequence_rev"`
DateUpdated time.Time `db:"date_updated" json:"date_updated"`
Date time.Time `db:"date" json:"date"`
Time time.Time `db:"time" json:"time"`
Ptyp2 string `db:"ptyp2" json:"ptyp2"`
WoQty int `db:"wo_qty" json:"wo_qty"`
IsActive int `db:"is_active" json:"is_active"`
IsTimeLock int `db:"is_time_lock" json:"is_time_lock"`
TimeLockTimestamp time.Time `db:"time_lock_timestamp" json:"time_lock_timestamp"`
ScrapReason string `db:"scrap_reason" json:"scrap_reason"`
ScrappedBy int `db:"scrapped_by" json:"scrapped_by"`
}
seq_test_stage.go
package models
import (
"time"
)
type SeqTestStage struct {
ID int64 `gorm:"primary_key" db:"id" json:"id"`
PdcptypeId int `db:"pdcptype_id" json:"pdcptype"`
Description string `db:"description" json:"description"`
LongDescription string `db:"long_description" json:"long_description"`
IsActive int `db:"is_active" json:"is_active"`
DateAdded time.Time `db:"date_added" json:"date_added"`
DateUpdated time.Time `db:"date_updated" json:"date_updated"`
TimeLock int `db:"time_lock" json:"time_lock"`
LockMinutes int `db:"lock_minutes" json:"lock_minutes"`
LockHours int `db:"lock_hours" json:"lock_hours"`
}