uptrace / bun

SQL-first Golang ORM
https://bun.uptrace.dev
BSD 2-Clause "Simplified" License
3.78k stars 231 forks source link

Issue using `->>` with JSONB column in SELECT query #821

Open robdasilva opened 1 year ago

robdasilva commented 1 year ago

I have a table with a JSONB typed column props. This is the query I use to extract a property from that column for each row:

var parentIds []string

err := db.
        WithNamedArg("ProdIdColName", bun.Ident("productId")).
        NewSelect().
        Model((*models.Product)(nil)).
        ColumnExpr("?TableAlias.props->>? AS ?ProdIdColName", "parentId").
        Limit(100).
        Scan(context.TODO(), &parentIds)

That gives me the following error though:

{
  "errorMessage": "sql: Scan error on column index 0, name \"productId\": invalid character '-' after top-level value",
  "errorType": "wrapError",
  "stackTrace": [...]
}

There seems to be an issue, that the string value of productId is regarded and attempted to be parsed as JSON. I am not sure, how I could fix/circumvent this. Can anyone point me into the right direction?

github-actions[bot] commented 4 days ago

This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed.