Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*string and *int cause left join not to return nil #7321

Open
Axb12 opened this issue Dec 19, 2024 · 1 comment
Open

*string and *int cause left join not to return nil #7321

Axb12 opened this issue Dec 19, 2024 · 1 comment
Assignees
Labels

Comments

@Axb12
Copy link

Axb12 commented Dec 19, 2024

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.

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
			}
@github-actions github-actions bot added the type:question general questions label Dec 19, 2024
@Axb12 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
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Dec 19, 2024
Copy link

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.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants