You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have two model structs. When I combine these two structs for querying, a problem occurs.
type User struct {
ID uuid.UUID `gorm:"primarykey;column:id"`
Name string `gorm:"column:name"`
}
func (User) TableName() string {
return "users"
}
type Order struct {
ID uuid.UUID `gorm:"primarykey;column:id"`
UserID uuid.UUID `gorm:"column:user_id"`
Name string `gorm:"column:name"`
}
func (Order) TableName() string {
return "orders"
}
It is correct when I use the following query. When the order is not matched, nil is returned. Like this user: {31e8f5fe-9848-4088-b786-ff75f1ab7563 user 1} order: <nil>
user, order := &User{}, &Order{}
userTable, orderTable := user.TableName(), order.TableName()
var uo []struct {
User
*Order
}
err = db.Model(user).Select(
userTable+".*, "+orderTable+".*",
).Joins(
"LEFT JOIN "+orderTable+" ON "+userTable+".id = "+orderTable+".user_id",
).Where(userTable+".id = ?", "31e8f5fe-9848-4088-b786-ff75f1ab7563").Scan(&uo)
But when I use the following order struct. When the order is not matched, nil is not returned. Like this user: {31e8f5fe-9848-4088-b786-ff75f1ab7563 user 1} order: &{00000000-0000-0000-0000-000000000000 00000000-0000-0000-0000-000000000000 <nil>}
type Order struct {
ID uuid.UUID `gorm:"primarykey;column:id"`
UserID uuid.UUID `gorm:"column:user_id"`
Name *string `gorm:"column:name"`
}
But *uuid.UUID will still return nil. If it is *string or *int etc., it will not return nil.
Expected answer
I want to know why this is happening. It makes my logic for judging whether the order matches inconsistent.
I tried to modify it in field.go like this, and it worked in my case. I don't know what this will affect.
fallbackSetter := func(ctx context.Context, value reflect.Value, v interface{}, setter func(context.Context, reflect.Value, interface{}) error) (err error) {
if v == nil {
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
} else {
reflectV := reflect.ValueOf(v)
if reflectV.Elem().Kind() == reflect.Ptr && reflectV.Elem().IsNil() {
return
}
The text was updated successfully, but these errors were encountered:
Axb12
changed the title
For this question, I did the following: https://github.com/go-gorm/gorm/issues/7317
*string and *int cause left join not to return nil
Dec 19, 2024
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
This is my example address:
https://github.com/Axb12/playground.git
Description
I have two model structs. When I combine these two structs for querying, a problem occurs.
It is correct when I use the following query. When the order is not matched, nil is returned. Like this
user: {31e8f5fe-9848-4088-b786-ff75f1ab7563 user 1} order: <nil>
But when I use the following order struct. When the order is not matched, nil is not returned. Like this
user: {31e8f5fe-9848-4088-b786-ff75f1ab7563 user 1} order: &{00000000-0000-0000-0000-000000000000 00000000-0000-0000-0000-000000000000 <nil>}
But *uuid.UUID will still return nil. If it is *string or *int etc., it will not return nil.
Expected answer
I want to know why this is happening. It makes my logic for judging whether the order matches inconsistent.
I tried to modify it in field.go like this, and it worked in my case. I don't know what this will affect.
The text was updated successfully, but these errors were encountered: