Masterminds / squirrel

Fluent SQL generation for golang
Other
6.67k stars 458 forks source link

Where clause compare column of table #367

Open bohrasankalp opened 9 months ago

bohrasankalp commented 9 months ago

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

lann commented 9 months ago

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")}
bohrasankalp commented 9 months ago

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 commented 9 months ago

@lann any update on this?