How to specify a model to model association but without any dependency? - go

i got two models in GORM as following:
type (
Todo struct {
gorm.Model
EventId int64 `json:"event_id" form:"event_id" binding:"required"`
UserId uint `json:"user_id" form:"user_id"`
DoneUserId uint `json:"done_user_id" form:"done_user_id"`
Groups string `json:"groups" form:"groups"`
DoneAt *time.Time `json:"done_at" form:"done_at"`
TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
Priority uint `json:"priority" form:"priority" binding:"required"`
Done bool `json:"done" form:"done"`
Title string `json:"title" form:"title" binding:"required"`
Description string `json:"description" form:"description"`
CreatorId uint `json:"creator_id"`
ChangerId uint `json:"changer_id"`
Event Event
}
)
and
type (
Event struct {
gorm.Model
CustomerId uint `json:"customer_id" form:"customer_id" binding:"required"`
AddressId uint `json:"address_id" form:"address_id" binding:"required"`
UserId uint `json:"user_id" form:"user_id"`
EventType string `json:"event_type" form:"event_type" binding:"required"`
ContactPerson string `json:"contact_person" form:"contact_person"`
Title string `json:"title" form:"title" binding:"required"`
Description string `gorm:"type:text" json:"description" form:"description"`
Calculated bool `json:"calculated" form:"calculated"`
Goodwill bool `json:"goodwill" form:"goodwill"`
Billable bool `json:"billable" form:"billable"`
EventBegin time.Time `json:"event_begin" form:"event_begin" binding:"required"`
EventEnd time.Time `json:"event_end" form:"event_end" binding:"required"`
PartsJson string `gorm:"type:text" json:"parts_json" form:"parts_json"`
FieldsJson string `gorm:"type:text" json:"fields_json" form:"fields_json"`
CreatorId uint `json:"creator_id"`
ChangerId uint `json:"changer_id"`
Todos []Todo
Customer Customer
}
)
When I save a new Todo with an event_id set, then I get many errors regarding empty fields within the event object. Thats right, because I do not fill the event object I just set the event_id in the todo object. So my question is, is there a way how I can disable these validations?
The association from Todo to Event is just for query reasons, that I get a nested Event-Object in Todo-Object - or better said I get the nested object in the json.

I would personally try do it like this:
type MinimalTodo struct {
gorm.Model
EventId int64 `json:"event_id" form:"event_id" binding:"required"`
UserId uint `json:"user_id" form:"user_id"`
DoneUserId uint `json:"done_user_id" form:"done_user_id"`
Groups string `json:"groups" form:"groups"`
DoneAt *time.Time `json:"done_at" form:"done_at"`
TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
Priority uint `json:"priority" form:"priority" binding:"required"`
Done bool `json:"done" form:"done"`
Title string `json:"title" form:"title" binding:"required"`
Description string `json:"description" form:"description"`
CreatorId uint `json:"creator_id"`
ChangerId uint `json:"changer_id"`
}
func (MinimalTodo) TableName() string {
return "todos"
}
type Todo struct {
MinimalTodo
Event
}
Not sure if it'll work with Gorm. Otherwise, I'd probably see how much work it would be to just change Event to a pointer:
type Todo struct {
gorm.Model
EventId int64 `json:"event_id" form:"event_id" binding:"required"`
UserId uint `json:"user_id" form:"user_id"`
DoneUserId uint `json:"done_user_id" form:"done_user_id"`
Groups string `json:"groups" form:"groups"`
DoneAt *time.Time `json:"done_at" form:"done_at"`
TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
Priority uint `json:"priority" form:"priority" binding:"required"`
Done bool `json:"done" form:"done"`
Title string `json:"title" form:"title" binding:"required"`
Description string `json:"description" form:"description"`
CreatorId uint `json:"creator_id"`
ChangerId uint `json:"changer_id"`
Event *Event
}

Related

invalid field found for struct models.User's field Identities: define a valid foreign key for relations or implement the Valuer/Scanner interface

Always got an error when I add the relationship to the User in GitUser struct.
type GitUser struct {
ID uint `json:"id" gorm:"primaryKey"`
ExternalID string
JobID uint
UserID uint
User User `gorm:"foreignKey:UserID;references:ID"`
Job Job `gorm:"foreignKey:JobID;references:ID"`
}
type User struct {
ID uint `json:"id" gorm:"primaryKey"`
ExternalId *string
Username string
Email string
Name string
HashedPassword *string
AvatarUrl *string
IsAdmin bool
LastSignInAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
IsArchived bool
GitUser []GitUser
}
I'm trying to make a mutual relationship between User and GitUser

How to fix unsupported relations for schema error with gorm.Preload

I keep getting error Technician: unsupported relations for schema Ticket for this struct schema and query? What can I do to get make this Preload query works?
Or at least how can debug this issue? The error is pretty bare minimal and I have read the gorm preload page https://gorm.io/docs/preload.html, and don't get what I did wrong?
type Ticket struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ShopID uuid.UUID `json:"shopID"`
Archived bool `json:"archived"`
Services []string `json:"services"`
Price int `json:"price"`
Location int `json:"location"`
Checkedout bool `json:"checkedout"`
TechnicianID uuid.UUID `json:"technicianId"`
Technician Technician `json:"technician"`
TechnicianPartnerID *uuid.UUID `json:"technicianPartnerId"`
LastUpdatedBy uuid.UUID `json:"lastupdatedBy"`
}
type Technician struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ShopID uuid.UUID `json:"shopID"`
Name string `json:"name"`
Active bool `json:"active"`
}
dbQuery := t.Db.Orm.WithContext(ctx).Table("tickets").Preload("Technician")
You are not using the standard gorm.Model for the keys (from the docs):
type Model struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
Gorm uses this to identify joins.
Changing your keys with the gorm:"primaryKey" indicator should fix the issue.
Or alternative: use gorm.Model:
type Ticker struct {
gorm.Model
ShopID uuid.UUID `json:"shopID"`
Archived bool `json:"archived"`
Services []string `json:"services"`
Price int `json:"price"`
Location int `json:"location"`
Checkedout bool `json:"checkedout"`
TechnicianID uuid.UUID `json:"technicianId"`
Technician Technician `json:"technician"`
TechnicianPartnerID *uuid.UUID `json:"technicianPartnerId"`
LastUpdatedBy uuid.UUID `json:"lastupdatedBy"`
}
type Technician struct {
gorm.Model
ShopID uuid.UUID `json:"shopID"`
Name string `json:"name"`
Active bool `json:"active"`
}

using smart select in gorm with many 2 many relation

I want to use smart select when querying rooms table. When use the original struct it works fine but when use smart select it fails.
Here is my schema
type Room struct {
gorm.Model
ID uint `json:"id" gorm:"primary_key"`
Hash string `json:"hash" binding:"required" gorm:"not null:true"`
Users []User `json:"users" gorm:"many2many:room_users"`
Messages []Message `json:"messages"`
}
type RoomAPI struct {
ID uint `json:"id" gorm:"primary_key"`
Hash string `json:"hash" binding:"required" gorm:"not null:true"`
Users []User `json:"users" gorm:"many2many:room_users"`
//Messages []Message `json:"messages" gorm:"foreignKey:RoomID"`
}
func (RoomAPI) TableName() string {
return "rooms"
}
type User struct {
gorm.Model
ID uint `json:"id" gorm:"primary_key"`
Name string `json:"name" binding:"required" gorm:"not null:true"`
Phone string `json:"phone"`
Email string `json:"email" binding:"required,email" gorm:"not null:true"`
Password string `json:"password" binding:"required,min=8" gorm:"not null:true"`
Gender string `json:"gender" binding:"Enum=male_female" gorm:"type:gender;not null:true;default:male"`
Rooms []Room `json:"rooms" gorm:"many2many:room_users"`
}
type Message struct {
gorm.Model
ID uint `json:"id" gorm:"primary_key"`
Text string `json:"text" binding:"required" gorm:"not null:true"`
UserID uint `json:"user_id" gorm:"not null:true"`
RoomID uint `json:"room_id" gorm:"not null:true"`
}
here is my query
var rooms []RoomAPI
user := User{ID: userId}
err := db.DB.Model(&user).Preload("Users").
Association("Rooms").
Find(&rooms)
if err != nil {
log.Printf("err -> %+v", err)
return err, nil
}
return nil, rooms
I want to get rooms with selected fields with smart select but it fails

invalid field found for struct field , need to define a foreign key for relations or it need to implement the Valuer/Scanner interface

Omit not working.
Retrieve Error:
invalid field found for struct deliveryFood/models.Restaurant's field
DeliveryZone, need to define a foreign key for relations or it need to
implement the Valuer/Scanner interface
type Restaurant struct {
ID uint
Name string `json:"name"`
EmployeeId uint `json:"employee_id"`
Phone string `json:"phone"`
Address string `json:"address"`
ImagesUrl *string `json:"images_url"`
Position string `json:"position"`
WorkDays string `json:"work_days"`
StartWorkTime string `json:"start_work_time"`
EndWorkTime string `json:"end_work_time"`
Blocked bool `json:"blocked"`
DeliveryZone []*DeliveryZone `json:",omitempty"`
}
type DeliveryZone struct {
ID uint `json:"id"`
RestaurantId uint `json:"restaurant_id"`
Zone string `json:"zone"`
Price float32 `sql:"-"`
}
err := GetDB().Omit(clause.Associations).Model(Restaurant{}).Create(map[string]interface{} {
"name": rest.Name,
"EmployeeId": rest.EmployeeId,
"Phone": rest.Phone,
"Address": rest.Address,
"ImagesUrl": rest.ImagesUrl,
"WorkDays": rest.WorkDays,
"StartWorkTime": rest.StartWorkTime,
"EndWorkTime": rest.EndWorkTime,
"Blocked": rest.Blocked,
"Position": clause.Expr{
SQL: "ST_GeomFromText(?)",
Vars: []interface{}{fmt.Sprintf("POINT((%s))", rest.Position)},
},
}).Error
Change RestaurantId to RestaurantID in DeliveryZone struct.
type Restaurant struct {
ID uint
Name string `json:"name"`
EmployeeId uint `json:"employee_id"`
Phone string `json:"phone"`
Address string `json:"address"`
ImagesUrl *string `json:"images_url"`
Position string `json:"position"`
WorkDays string `json:"work_days"`
StartWorkTime string `json:"start_work_time"`
EndWorkTime string `json:"end_work_time"`
Blocked bool `json:"blocked"`
DeliveryZone []*DeliveryZone `json:",omitempty"`
}
type DeliveryZone struct {
ID uint `json:"id"`
RestaurantID uint `json:"restaurant_id"`
Zone string `json:"zone"`
Price float32 `sql:"-"`
}
Or you can define the foreignkey manually by adding foreignKey tag in Restaurant struct. e.g.
DeliveryZone []*DeliveryZone json:",omitempty" gorm:"foreignKey:RestaurantId"
try
DeliveryZone []*DeliveryZone `gorm:"-"`
https://gorm.io/docs/models.html -> ctrl+F -> ignore this field

How to solve gorm many2many error1062

I have a many2many relation between news and tag. Whenever I tried to assign news to a tag it gives me an error Error 1062: Duplicate entry '5' for key 'news_id'
My model look like this:
type Tag struct {
ID uint64 `json:"id" gorm:"primary_key;unique;AUTO_INCREMENT"`
Title string `json:"title" gorm:"varchar(20)" binding:"required"`
News []*News `json:"news" gorm:"many2many:news_tags;association_foreignkey:ID;foreignkey:ID"`
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
}
// News model
type News struct {
ID uint64 `json:"id" gorm:"primary_key;unique;AUTO_INCREMENT"`
Title string `json:"title" gorm:"varchar(20)" binding:"required"`
Body string `json:"body" gorm:"varchar(500)" binding:"required"`
Status string `json:"status" gorm:"varchar(5)"` // 1 = publish; 2 = draft; 3 = deleted;
Tags []*Tag `json:"tags" gorm:"many2many:news_tags;association_foreignkey:ID;foreignkey:ID"`
Topics []*Topic `json:"topics" gorm:"many2many:news_topics"`
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
}
// My code when trying to assign new tag
func (n *News) AssignTags(tag Tag) {
db.DB().Model(n).Association("Tags").Append(tag)
}

Resources