squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

Where clause compare column of table

Open bohrasankalp opened this issue 2 years ago • 5 comments

The update query is

UPDATE videos v SET channel = $1 FROM channels c WHERE c.channel_id = $2 AND v.channel_id = $3

Builder

var channelID int
query, args, _ := sq.Update("videos v").Set("channel", "c.id").From("channels c").
		Where(sq.Eq{"c.channel_id": "v.channel_id", "v.channel_id": channelID}).PlaceholderFormat(sq.Dollar).ToSql()

This makes args as "c.id" & "v.channel_id" and actually compares string

bohrasankalp avatar Oct 01 '23 11:10 bohrasankalp

Eq works this way on purpose for the common case of comparing with variable values. Try:

sq.Eq{"c.channel_id": sq.Expr("v.channel_id")}

lann avatar Oct 02 '23 13:10 lann

this gives error as Cannot encode squirrel.expr in simple protocol - squirrel.expr must implement driver.Valuer, pgtype.TextEncoder, or be a native type

bohrasankalp avatar Oct 02 '23 17:10 bohrasankalp

@lann any update on this?

bohrasankalp avatar Oct 05 '23 14:10 bohrasankalp