mysql
mysql copied to clipboard
Clause.(clause.Returning{}) dosen't work in a delete opertaion
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.
At first, I thought the database dose not support Returning. So I try it by raw sql, it works.
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
.
Partof callback.go/Execute function
statement.go/AddClause function
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.
Can you create a pr in go-gorm/playground to describe it ? https://github.com/go-gorm/gorm/blob/master/tests/delete_test.go#L248
@a631807682 OK, I'll try it.
@a631807682 Hello, I think I reproduced the problem I had in this pr https://github.com/go-gorm/playground/pull/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.
@Zacama I checked the playground and test is passed. Are you using mysql? It does not support returning.
@a631807682
Hello, I'm using Mariadb-10.4.18. And I using this sql to test it in datagrip.
It can return.
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,
})
@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,
})
@Zacama yes
@a631807682 It returns a lot of warning ...
I find that it has been called in gorm.Open()
So it prints a lof of warning log.
how about?
db, _ := gorm.Open(mysql.Open(dsn))
createCallback := db.Callback().Create()
createCallback.Clauses = []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"}
@a631807682 yeah, It can print right rawsql now.
Incredible... it can only print right right rawsql.
But when I test in mysql command line. It return correctly.
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
after gorm.Open
But I still don't know why writing like that doesn't return correctly
@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.
@Zacama I think we can keep it open and wait for someone to implement it.
@a631807682 OK, we keep it.