mysql icon indicating copy to clipboard operation
mysql copied to clipboard

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

Open Zacama opened this issue 2 years ago • 12 comments

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.

Zacama avatar Jul 25 '22 06:07 Zacama

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 avatar Jul 27 '22 03:07 a631807682

@a631807682 OK, I'll try it.

Zacama avatar Jul 27 '22 03:07 Zacama

@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 avatar Aug 04 '22 06:08 Zacama

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

a631807682 avatar Aug 04 '22 16:08 a631807682

@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.

Zacama avatar Aug 05 '22 06:08 Zacama

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 avatar Aug 05 '22 06:08 a631807682

@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 avatar Aug 05 '22 07:08 Zacama

@Zacama yes

a631807682 avatar Aug 05 '22 07:08 a631807682

@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.

Zacama avatar Aug 05 '22 07:08 Zacama

how about?

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

a631807682 avatar Aug 05 '22 08:08 a631807682

@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 avatar Aug 08 '22 02:08 Zacama

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 avatar Aug 08 '22 04:08 Zacama

@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 avatar Aug 17 '22 00:08 Zacama

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

a631807682 avatar Aug 17 '22 08:08 a631807682

@a631807682 OK, we keep it.

Zacama avatar Aug 18 '22 03:08 Zacama