go-pg / pg

Golang ORM with focus on PostgreSQL features and performance
https://pg.uptrace.dev/
BSD 2-Clause "Simplified" License
5.67k stars 404 forks source link

add AppendColumn method #2006

Open bohdand-weka opened 4 months ago

bohdand-weka commented 3 months ago

I'm curious what the use case for this is, as the AppendColumn method itself only adds columns that are present on the actual model you're working with. If you are joining between two tables, it would only let you append columns from the base table (whatever was passed to Model(...). Which all of the columns from that model would already be present by default?

Would you be able to provide an example use case for this as well as tests?

The use case is simple.

Imagine we have dynamic columns selection. By default Column() will override all model columns when called first time.

So in our scenario we don't want to override default columns if Column wasn't called with Alias.*, instead we want to just append one more column from our calculation, like aggregation or whatever. If in our dynamic query there was already Column called - AppendColumn won't touch them, so it does exactly how it's called - appends column.

And it will work for joins as well (as like Column does), why not?