Skip to content

Gorm 1.6.0 breaking change for where conditions with joins #7489

@nikiepaydk

Description

@nikiepaydk

GORM Playground Link

go-gorm/playground#809

Description

There is introduced a breaking change in gorm v1.6.0. We have code similar to this snippet:

func Test_gorm_query(t *testing.T) {
	// Arrange
	type Parent struct {
		gorm.Model
		Field string
	}

	type Child struct {
		gorm.Model
		ParentId uint
		Parent   Parent
		Data     string
	}

	setupDatabaseEnvironment(&Parent{}, &Child{})

	var output Parent

	Database.DB().
		Joins("LEFT JOIN children ON children.parent_id = parents.id").
		Where(map[string]any{
			"children.data": "gorm",
		}).
		Limit(1).
		Find(&output)
}

And when upgrading from 1.5.7 to 1.6.0 the resulting query changes causing an error.

Before:
- gorm.io/driver/mysql v1.5.7
- gorm.io/driver/postgres v1.5.11
- gorm.io/driver/sqlite v1.5.7
- gorm.io/gorm v1.25.12
- gorm.io/plugin/dbresolver v1.5.3

OK:
SELECT `parents`.`id`,`parents`.`created_at`,`parents`.`updated_at`,`parents`.`deleted_at`,`parents`.`field` FROM `parents` LEFT JOIN children ON children.parent_id = parents.id WHERE `children`.`data` = "gorm" AND `parents`.`deleted_at` IS NULL LIMIT 1

After:
- gorm.io/driver/mysql v1.6.0
- gorm.io/driver/postgres v1.6.0
- gorm.io/driver/sqlite v1.6.0
- gorm.io/gorm v1.30.0
- gorm.io/plugin/dbresolver v1.6.0

no such column: parents.children.data:
SELECT `parents`.`id`,`parents`.`created_at`,`parents`.`updated_at`,`parents`.`deleted_at`,`parents`.`field` FROM `parents` LEFT JOIN children ON children.parent_id = parents.id WHERE `parents`.`children`.`data` = "gorm" AND `parents`.`deleted_at` IS NULL LIMIT 1

Expected Behaviour

We would expect the same behaviour to continue between non-major versions.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions