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

postgresql : UPDATE query formed incorrectly, update ... set ... FROM ... where #7055

Closed
alkuma opened this issue Jun 7, 2024 · 11 comments
Closed
Assignees
Labels

Comments

@alkuma
Copy link

alkuma commented Jun 7, 2024

GORM Playground Link

go-gorm/playground#746 provided by @ajhodges

Description

I have an update executed within a database transaction and that is leading to the following faulty query

UPDATE users 
SET col1 = value,
col2  = value,
...
col 20  = value
FROM users -- WRONG! FROM must not be present in the update query 
WHERE user_id = value 
AND user_role.is_del = 0

The error obtained from postgres is

ERROR: table name "uaa_role" specified more than once (SQLSTATE 42712)

I have another location where this query works fine, the only difference I see is that at SELECT has been performed on the same table before this update.

Is this a case of the stale FROM clause from the SELECT lying in the parse tree when the UPDATE is query is being formed? If so what is a workaround for this?

this is using

	gorm.io/driver/postgres v1.5.7
	gorm.io/gorm v1.25.10

Here is a sequence of queries that ran before getting to this faulty update query. The sequence is edited by hand for brevity


[rows:1] SELECT count(*) FROM users WHERE user_id = value AND users.is_del = 0

[rows:26] SELECT * FROM users WHERE ("tenant_id","user_id") IN (('Value','Value'))

[rows:1] SELECT * FROM users WHERE user_id = 'value' AND users.is_del = 0 ORDER BY user_id 

[rows:1] SELECT count(*) FROM users WHERE users.is_del = 0


followed by the faulty query


Are there any changes that happened which could lead to the previous SELECT query's parse tree is still lying around and polluting the parser input for the update?

If so is there a workaround for this?

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Jun 7, 2024
Copy link

github-actions bot commented Jun 7, 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.ioSearch Before Asking

@marek-veber
Copy link

I have the same issue (with gorm.io/driver/postgres v1.5.7& gorm.io/gorm v1.25.10). It's based on:
db.Model(...).Find(....).Update(...)

Copy link

github-actions bot commented Jun 7, 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.ioSearch Before Asking

@ajhodges
Copy link

@alkuma I added a playground test case for this here, I'm facing the same issue: go-gorm/playground#746

@ajhodges
Copy link

ajhodges commented Jun 24, 2024

FWIW it looks like this is a regression that started in gorm.io/driver/postgres v1.5.3 (v1.5.2 does not have this issue)

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

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

@alkuma
Copy link
Author

alkuma commented Jun 25, 2024

@ajhodges go-gorm/playground#746 is a much more concise test case than mine. Let me add this to the original issue report thank you

@github-actions github-actions bot added type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Jun 25, 2024
@gabizou
Copy link

gabizou commented Jun 25, 2024

Just got bit by this issue as well, it's even more apparent when using soft deletion "updates".

@a631807682
Copy link
Member

use Session api
https://gorm.io/docs/method_chaining.html#Reusability-and-Safety

@ajhodges
Copy link

@a631807682 I updated the example here with a session: go-gorm/playground#746

It has no effect. I still get this error:

2024/06/29 16:28:21 /Users/adamhodges/src/gorm-playground/main_test.go:20 ERROR: table name "users" specified more than once (SQLSTATE 42712)
[0.635ms] [rows:1] UPDATE "users" SET "name"='jinzhu 2',"updated_at"='2024-06-29 16:28:21.723' FROM "users" WHERE "users"."id" = 1 AND "users"."deleted_at" IS NULL AND "id" = 1
--- FAIL: TestGORM (0.00s)
    main_test.go:21: Failed, got error: ERROR: table name "users" specified more than once (SQLSTATE 42712)
FAIL
exit status 1
FAIL	gorm.io/playground	0.580s

Again, this seems to be a regression in gorm.io/driver/postgres v1.5.3+

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

6 participants