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

Clause.(clause.Returning{}) dosen't work in a delete opertaion #85

Closed
Zacama opened this issue Jul 25, 2022 · 15 comments
Closed

Clause.(clause.Returning{}) dosen't work in a delete opertaion #85

Zacama opened this issue Jul 25, 2022 · 15 comments
Assignees

Comments

@Zacama
Copy link

Zacama commented Jul 25, 2022

Clause.(clause.Returning{}) dosen't active in a delete opertaion

Version: gorm 1.23.8 and golang 1.18.3

Sorry for my poor English...

I try to use Clause.(clause.Returning{}) to returning the obj which has been deleted. But It doesn't work.
image
At first, I thought the database dose not support Returning. So I try it by raw sql, it works.
image
Then, I try to find the problem by reading source code and debug it. Limited by my skill level, I can only find out the final raw sql (generated by gorm) does not contain RETURNING .
image
Partof callback.go/Execute function
image
statement.go/AddClause function
image
But it can be seen from the screenshot that RETURNING is added to db.Statement.Clauses map but it seems like no subsequent code to use it.
I don't know which step is wrong. If anyone could help me take a look, it would be greatly appreciated.

The document you expected this should be explained

Expected answer

Just want got the return successfully.

@a631807682
Copy link
Member

@Zacama
Copy link
Author

Zacama commented Jul 27, 2022

@a631807682 OK, I'll try it.

@Zacama
Copy link
Author

Zacama commented Aug 4, 2022

@a631807682 Hello, I think I reproduced the problem I had in this pr go-gorm/playground#503 . But at the same time I encountered a new problem. When I try to create a new 'process', the primaryKey is blank, but I have set it autoIncrement and not null.

@a631807682
Copy link
Member

@Zacama I checked the playground and test is passed.
Are you using mysql? It does not support returning.

@Zacama
Copy link
Author

Zacama commented Aug 5, 2022

@a631807682
image
Hello, I'm using Mariadb-10.4.18. And I using this sql to test it in datagrip.
180708041-a50bc232-e8a6-458f-a381-24d6f194c0ee
It can return.

@a631807682 a631807682 transferred this issue from go-gorm/gorm Aug 5, 2022
@a631807682
Copy link
Member

a631807682 commented Aug 5, 2022

returning may need to be specified depending on the version, need confirm.
https://github.com/go-gorm/mysql/blob/master/mysql.go#L41

@Zacama I didn't try it, but you should be able to use like this

	callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
		CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"},
		QueryClauses:  mysql.QueryClauses,
		UpdateClauses: mysql.UpdateClauses,
		DeleteClauses: mysql.DeleteClauses,
	})

@Zacama
Copy link
Author

Zacama commented Aug 5, 2022

@a631807682 I want to confirm that do you mean I should able to use like this?

        db, _ := gorm.Open(mysql.Open(dsn))
        // after init, register it. 
	callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
		CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"},
		QueryClauses:  mysql.QueryClauses,
		UpdateClauses: mysql.UpdateClauses,
		DeleteClauses: mysql.DeleteClauses,
	})

@a631807682
Copy link
Member

@Zacama yes

@Zacama
Copy link
Author

Zacama commented Aug 5, 2022

@a631807682 It returns a lot of warning ...
image

I find that it has been called in gorm.Open()
image
So it prints a lof of warning log.

@a631807682
Copy link
Member

how about?

db, _ := gorm.Open(mysql.Open(dsn))
createCallback := db.Callback().Create()
createCallback.Clauses = []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"}

@Zacama
Copy link
Author

Zacama commented Aug 8, 2022

@a631807682 yeah, It can print right rawsql now.
image
Incredible... it can only print right right rawsql.
image
image
But when I test in mysql command line. It return correctly.
image

@Zacama
Copy link
Author

Zacama commented Aug 8, 2022

I tried to find out what the problem is, but I can't find it...
So I try to use rawsql. Amazingly, it worked and it does not need execute this
image
after gorm.Open
image
image

But I still don't know why writing like that doesn't return correctly

@Zacama
Copy link
Author

Zacama commented Aug 17, 2022

@a631807682 Really, thank you very very much for helping me solve this problem😊. I achieved my goal. I don't quite understand the process of closing the issue. If I can close this issue, I'm ready to close it.

@a631807682
Copy link
Member

@Zacama I think we can keep it open and wait for someone to implement it.

@Zacama
Copy link
Author

Zacama commented Aug 18, 2022

@a631807682 OK, we keep it.

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

No branches or pull requests

3 participants