Accessing column in junction table in GORM - go

I have three tables users, todos and user_todos:
type User struct {
ID uint64
Email string
Todos []Todo `gorm:"many2many:user_todos"`
}
type Todo struct {
ID uint64
Title string
}
type UserTodos struct {
UserID uint64
TodoID uint64
Done bool
}
Now I want to get all users with all todos. db.Preload("Todos").Find(&users) does the trick for that.
But what if I want to get all todos and see if they are already done by the user?
I tried to create a SQL view checked_todos which joins the user_todos table and exchange the Todos attribute in the User struct with a CheckedTodo, which just extends Todo with a boolean field. Preloading never access the right table.
Couldn't find any hints in the GORM documentation how to access the columns in the junction table.

You might be able to achieve what you want with custom preloading. Two options you might want to try:
Option 1
db.Preload("Todos", func(grm *gorm.DB) *gorm.DB {
return grm.Where("user_todos.done = 1")
}).Find(&users)
Option 2
db.Preload("Todos", func(grm *gorm.DB) *gorm.DB {
return grm.Joins("INNER JOIN user_todos ON user_todos.todo_id = todos.id AND user_todos.done = 1")
}).Find(&users)

Related

Gorm joined tables selects only few field

I have some of this code
type User struct {
Id uint `gorm:"column:id"`
Name string `gorm:"column:name"`
WalletId uint `gorm:"column:wallet_id"`
Wallet Wallet `gorm:"foreignkey:club_id"`
}
type Wallet struct {
Id uint `gorm:"column:id"`
Money uint `gorm:"column:money"`
Valute string `gorm:"column:valute"`
}
I can query users and their wallets like this
var users []users
db.Joins("Wallet").Find(&users)
this will generate stmt like this
SELECT user.id, user.name, user.wallet_id, wallet.id, wallet.Money, wallet.Valute from user join wallet on user.wallet_id = wallet.id
I need to query all users and only `Valute' field from their wallets
I want to use a gorm like
var users []users
db.Joins("Wallet", db.Select("Valute")).Find(&users)
for generate this stmt
SELECT user.id, user.name, user.wallet_id, wallet.Valute from user join wallet on user.wallet_id = wallet.id
For choose a wallet field. But it doesn't work at all. Although there is such a use in the function annotation
// Joins specify Joins conditions
// db.Joins("Account").Find(&user)
// db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu#example.org").Find(&user)
// db.Joins("Account", DB.Select("id").Where("user_id = users.id AND name = ?", "someName").Model(&Account{}))
func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
In eager loading by Preload i can choose fields like
db.Limit(1000).Preload("Wallet",
func(db *gorm.DB) *gorm.DB {
return db.Select("valute")
},
).Find(&users)
But this isnt good usage for me, it has a bad perfomance when querying one object. I like to know how can i do the same with Joins
PS: I dont want to use a lightweigh model for wallet which queries only valute, i want to use single model for db presentation
I also want Gorm to support this feature but the best solution I came up with is Smart Select but it requires us to manually write query and select fields
type CustomUser struct {
Id uint `gorm:"column:id"`
Name string `gorm:"column:name"`
Valute string `gorm:"column:valute"`
}
var users []CustomUser
db.Raw("SELECT users.id, users.name, Wallet.valute FROM `users` LEFT JOIN `wallets` `Wallet` ON `users`.`wallet_id` = `Wallet`.`id`").Find(&users)

Foreign Key Constraint in gorm

I want to create a Booking-Class relationship, where every Booking can be assigned only one Class.
type Class struct {
Id int `json:"id"`
Name string `json:"name"`
}
type Booking struct {
Id int `json:"id"`
User string `json:"user"`
Members int `json:"members"`
ClassId int `json:"classid"`
}
I understand that it is similar to gorm's "belongs-to" relationship explained here https://gorm.io/docs/belongs_to.html but I was wondering if it's possible to achieve "foreign key constraint" without defining the field of type Class inside the Booking model (only ClassId int). To make sure that non-existent ClassId-s are used I defined functions:
func Find(slice []int, val int) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
func GetClassKeys(d *gorm.DB) []int {
var keys []int
rows, _ := d.Raw(`SELECT id
FROM classes`).Rows()
defer rows.Close()
for rows.Next() {
var key int
rows.Scan(&key)
keys = append(keys, key)
}
return keys
}
And perform this check before creating/updating a booking:
if !Find(GetClassKeys(db), booking.ClassId) {
log.Println("Wrong class id value")
return
}
But this doesn't handle the case of removed class id (which regular databases do automatically). I was wondering is there a way to achieve a normal database foreign key functionality with gorm by simply referencing the primary key of Class in a User model? Thanks in advance
I don't think the Migrator tool will help you because it assumes you're going to use the default Gorm patterns for defining relationships, and you've explicitly decided NOT to use these.
The only remaining option is to manage the constraint yourself, either through an SQL script or easier, some custom queries you run alongside your AutoMigrate call:
import "gorm.io/gorm/clause"
// somewhere you must be calling
db.AutoMigrate(&Class{}, &Booking{})
// then you'd have:
constraint := "fk_booking_classid"
var count int64
err := db.Raw(
"SELECT count(*) FROM INFORMATION_SCHEMA.table_constraints WHERE constraint_schema = ? AND table_name = ? AND constraint_name = ?",
db.Migrator().CurrentDatabase(),
"bookings",
constraint,
).Scan(&count).Error
// handle error
if (count == 0) {
err := db.Exec(
"ALTER TABLE bookings ADD CONSTRAINT ? FOREIGN KEY class_id REFERENCES classes(id)",
clause.Table{Name: constraint},
).Error
// handle error
}
Of course, this negates the advantage of having an automated migration (which means when things change like the field name, the constraint will be updated without changes to the migration code).
I'd be looking at why you don't want to define the foreign key as gorm expects, as this smells of you trying to use the database model directly as your JSON API Request/Response model, and that usually doesn't lead to a good end :)

How to create an autoincrement ID field

I have a set of employees defined in a JSON-file:
type PrimitiveEmployee struct {
PrimitiveID string `xml:"ID,attr"`
ContractID []string `xml:"ContractID"`
}
Both ID and ContractID is letters, but they are not necessarily given continuous sequence of letters. For instance, there might exist an employee with PrimitiveID=A and another with PrimitiveID=C, but not an employee with PrimitiveID=B.
I want to convert this data into:
type Employee struct {
ID int
PrimitiveID string
Contracts []Contract
}
The thing is, I want the the employee-ID to start at 0 and increment with one for each time the struct is initialized. Sort of like a an autoincrement ID in a database or iota in a enum.
The employee with PrimitiveID=A will then automatically be created with ID=0, while the employee with PrimitiveID=C will get ID=1.
I just cant figure out how to solve this in a struct.
Greatly appreciate any help or pointers here.
Create a custom type to manage the unique incrementing ID:
type autoInc struct {
sync.Mutex // ensures autoInc is goroutine-safe
id int
}
func (a *autoInc) ID() (id int) {
a.Lock()
defer a.Unlock()
id = a.id
a.id++
return
}
So you can use it in targeted places or at the package level:
var ai autoInc // global instance
You can then create "constructor" functions to leverage this:
func NewEmployee() *Employee {
return &Employee{
ID: ai.ID(),
}
}
Marshaling JSON data to Employee can then be performed and the ID will be preserved - provided the JSON data does not container an ID tag.
https://play.golang.org/p/0iTaQSzTPZ_j

Query with 'has one' association (one-to-one)

I'm playing a bit with Gorm while I'm trying to decide which ORM library fit the most for my needs.
Important to mention that I'm currently working with Sqlite.
Following the guide I created two structs:
type Color struct {
gorm.Model
UserID uint
Name string
}
//User struct define a basic user model
type User struct {
gorm.Model
Username string
Email string
FirstName string
LastName string
Password string
CreationDate time.Time
DOB time.Time
IgnoreMe int `gorm:"-"` // Ignore this field
Color Color `gorm:"foreignkey:ColorRefer"`
ColorRefer uint
}
when I'm creating a DB with
func CreateTables() {
user := dm.User{}
color := dm.Color{}
GormDB.CreateTable(&color)
GormDB.CreateTable(&user)
GormDB.Model(&user).AddForeignKey("ColorRefer", "colors(id)", "CASCADE", "CASCADE")
}
or with:
func CreateTables() {
GormDB.AutoMigrate(&dm.User{},&dm.Color{})
}
Sadly it's not working as I would of expect and create the foreign key automatically, but it's works when I do it manually.
My main problem is when I'm trying to query Users
//QueryByStructExample query users table by the struct non-zero (non-default) fields.
func QueryByStructExample(userStruct dm.User) []dm.User {
var results []dm.User
GormDB.Where(userStruct).Find(&results)
return results
}
I created the following function in a try to query users by email with the color property which is my color struct and I tried to play with a lot with the Model,Related and the Association functions, and nothing seems to work and (I'm avoiding to use join by purpose).
The end result is that it query my User but without the color (only with the ID in my colorRefer)
any suggestion?
Do you mean to preload the Color struct? If yes did you try to query it like that
GormDB.Preload('Color').Where(userStruct).Find(&results)

How to populate and embedded array with gorm?

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?
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 your user has no id property itself and there for cannot be related to items on the Pic table.
User should have a property Id which will be it's primary key and UserId is a foreign key on Pic that relates to it. Without the 'relation' between these two tables/entities there's no way you're going to return pics by querying users.
I'm not sure what all you need to do to make your code work since the example is incomplete but the first thing you need is an Id property which you should designate as a Primarykey with gorm annotations. You also should have annotations on the Pic struct saying UserId is a foreign key and Id is it's primary key.
Also, just fyi your array is not embedded. Embedding is a language feature which you're not using, if you embed the property it has no name and it's properties can be accessed directly from an instance of the embedding type.
I had these issues once. Then I used Join function.
See my example that works just fine:
type FileType struct {
Id int
}
type File struct {
Id int
FileType `xorm:"extends"`
}
file := File{Id: id}
has, err := eng.
Join("INNER", "FileType", "FileType.IdFileType = File.IdFileType").
Get(&file)
You probably know by now. You got to think as if you are creating a SQL table with 1-to-many relationship. Here is an example:
type Entry struct {
ID int
Name string
...
ContainerID int
}
type Container struct {
ID int
Tag int
Name string
...
Entries []Entry `gorm:"foreignkey:ContainerID"`
}
The trick is to populate it. I am yet to find how to make it in one try. For every such dependency, you got to run something like:
c := getContainerFromDB(...)
if err := getROConn().Model(c).Related(&c.Entries, "Entries").Error; err != nil {
return errors.Wrap(err, "error getting container field")
}
Try Preload
db.Limit(pagesize).Where("updated_at > ?", date).Preload("Pics").Find(&users)

Resources