gorm icon indicating copy to clipboard operation
gorm copied to clipboard

Additional `.Query` params silently fail if they are used on a raw query

Open pradovic opened this issue 8 months ago • 1 comments

I have been guilty of using mix of raw query with additional query parameters several times so far. Each time I found out later, during testing, that additional query params were actually not performed, but the query was executed without errors. The example:

       subQuery := `SELECT * from some_table 
        WHERE id = someID
        ORDER BY name`

	query := store.db.Tx(ctx).Raw(subQuery, params)

	if  someLogic {
		query = query.Where("additional_param= ?", additionalParam)
	}
	
	err = query.Find(&entities).Error
	

if someLogic is true, this query is executed without error, but the where clause is not executed.

Motivation

I think it would be great to either error out, or support adding the parameter at the correct place in the raw query. Let me know if this makes sense. I would be open on working on this as well (creating a PR).

pradovic avatar Jun 25 '24 15:06 pradovic