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

gdb: The order in which Safe and OmitEmptyWhere are called causes OmitEmptyWhere to fail. #3753

Closed
alanhi opened this issue Sep 2, 2024 · 4 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. need more details

Comments

@alanhi
Copy link

alanhi commented Sep 2, 2024

Go version

go version go1.22.4 windows/amd64

GoFrame version

v2.7.2

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

  1. dao中增加自定义方法
func (dao tbUserDao) CtxTenant(ctx context.Context) *gdb.Model {
	return dao.DB().Model(dao.Table()).Safe().Ctx(ctx).Where("`tenant_id` = ?", "888")
}
  1. 查询接口调用
record, err := dao.TbUser.CtxTenant(ctx).
	Safe(false).
	OmitEmptyWhere().
	// Safe(false).
	Where(do.TbUser{
		Name: "",
	}).
	One()

What did you see happen?

  1. 调用顺序.Safe(false).OmitEmptyWhere(),sql中name条件存在
SELECT * FROM `tb_user` WHERE (`tenant_id` = '888') AND (`name`='') LIMIT 1
  1. 调用顺序.OmitEmptyWhere().Safe(false),sql中name条件不存在
SELECT * FROM `tb_user` WHERE `tenant_id` = '888' LIMIT 1

看起来是.Safe(true).Where(xxx).Safe(false).OmitEmptyWhere()这种形式会出现问题

What did you expect to see?

fix

@alanhi alanhi added the bug It is confirmed a bug, but don't worry, we'll handle it. label Sep 2, 2024
@Issues-translate-bot Issues-translate-bot changed the title gdb: Safe与OmitEmptyWhere调用顺序导致OmitEmptyWhere失效 gdb: The order in which Safe and OmitEmptyWhere are called causes OmitEmptyWhere to fail. Sep 2, 2024
@wln32
Copy link
Member

wln32 commented Sep 13, 2024

初步判断原因可能是Safe机制问题

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Preliminary judgment is that the cause may be a problem with the Safe mechanism.

@gqcn
Copy link
Member

gqcn commented Sep 25, 2024

@alanhi Safe机制其实会在Model中设置标识,后续的所有链式操作都会判断是否使用浅拷贝。我理解你这里的问题是先通过链式操作修改了Model,这个时候Model已经脏了,后续再给Model设置Safe已经没有意义了。如果仍旧有问题请提交可运行的最小复现代码。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@alanhi The Safe mechanism will actually set the flag in Model, and all subsequent chain operations will determine whether to use shallow copy. I understand that your problem here is that you first modified the Model through a chain operation. At this time, the Model is already dirty, and it is meaningless to set Safe to the Model later. If you still have problems please submit a minimal working replica of the code.

@gqcn gqcn closed this as completed Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. need more details
Projects
None yet
Development

No branches or pull requests

4 participants