gorm icon indicating copy to clipboard operation
gorm copied to clipboard

Models with composite keys cannot be successfully removed using Delete(...)

Open jimlambrt opened this issue 4 years ago • 5 comments

GORM Playground Link

https://github.com/go-gorm/playground/pull/405

Description

I think, Gorm should generate valid sql and be able to delete resources with composite primary keys.

Thanks for all your amazing work on Gorm!

If I have time, I may do a bit of digging on the issue and see about opening a PR to fix it. No promises about the PR but I'll see if I can mange it next week.

jimlambrt avatar Nov 27 '21 16:11 jimlambrt

also refer https://github.com/go-gorm/gorm/issues/3585

jinzhu avatar Nov 29 '21 11:11 jinzhu

might need to replace the where clause for sqlite.

jinzhu avatar Nov 29 '21 11:11 jinzhu

This is also biting us for testing.

awmcclain avatar Feb 08 '22 16:02 awmcclain

Is this still an issue? If so maybe only in SQLite? I am using postgres where it works as expected. So maybe update the Issue to include ... on sqlite?

clausMeko avatar Jul 15 '22 06:07 clausMeko

I ran the test from the above playground link and it passed for sqlite. Heres the go.mod diff after running the test locally

diff --git a/go.mod b/go.mod
index 28f863c..f57a261 100644
--- a/go.mod
+++ b/go.mod
@@ -3,16 +3,19 @@ module gorm.io/playground
 go 1.16
 
 require (
+       github.com/denisenkom/go-mssqldb v0.12.2 // indirect
        github.com/go-kit/kit v0.10.0 // indirect
+       github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
        github.com/hashicorp/go-secure-stdlib/base62 v0.1.2 // indirect
-       github.com/jackc/pgx/v4 v4.14.0 // indirect
-       golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
+       github.com/jackc/pgx/v4 v4.17.2 // indirect
+       github.com/mattn/go-sqlite3 v1.14.15 // indirect
+       golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
        golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
-       gorm.io/driver/mysql v1.2.0
-       gorm.io/driver/postgres v1.2.2
-       gorm.io/driver/sqlite v1.2.6
-       gorm.io/driver/sqlserver v1.2.1
-       gorm.io/gorm v1.22.3
+       gorm.io/driver/mysql v1.3.6
+       gorm.io/driver/postgres v1.3.9
+       gorm.io/driver/sqlite v1.3.6
+       gorm.io/driver/sqlserver v1.3.2
+       gorm.io/gorm v1.23.8
 )
 
 replace gorm.io/gorm => ./gorm

test log

testing sqlite...
2022/09/15 05:45:55 testing sqlite3...

2022/09/15 05:45:56 ~/go/pkg/mod/gorm.io/driver/[email protected]/migrator.go:45 SLOW SQL >= 200ms
[1397.456ms] [rows:0] DROP TABLE IF EXISTS `companies`
=== RUN   TestGORM

2022/09/15 05:45:56 ~/playground/main_test.go:16
[4.526ms] [rows:1] INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`) VALUES ("2022-09-15 05:45:56.896","2022-09-15 05:45:56.896",NULL,"jinzhu",0,NULL,NULL,NULL,false) RETURNING `id`

2022/09/15 05:45:56 ~/playground/main_test.go:19
[0.634ms] [rows:1] SELECT * FROM `users` WHERE `users`.`id` = 1 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1

2022/09/15 05:45:56 ~/playground/main_test.go:35
[3.510ms] [rows:1] INSERT INTO `model_with_composite_keys` (`key_one`,`key_two`) VALUES ("fBvutjiGPO","qxJJEIR0o2")

2022/09/15 05:45:56 ~/playground/main_test.go:38
[3.891ms] [rows:1] DELETE FROM `model_with_composite_keys` WHERE (`model_with_composite_keys`.`key_one`,`model_with_composite_keys`.`key_two`) IN (("fBvutjiGPO","qxJJEIR0o2"))
--- PASS: TestGORM (0.01s)
PASS
ok  	gorm.io/playground	1.681s

rohfle avatar Sep 14 '22 17:09 rohfle