Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid input syntax for integer: \"{349,350}\" #515

Closed
fdelbos opened this issue Oct 7, 2016 · 2 comments
Closed

invalid input syntax for integer: \"{349,350}\" #515

fdelbos opened this issue Oct 7, 2016 · 2 comments

Comments

@fdelbos
Copy link

fdelbos commented Oct 7, 2016

Hi,

I have a an error with the following function:

func ByOwnerIDs(owner user.ID, ids ...int64) ([]Form, error) {
    const query = `
    select * from forms_data where owner_id = $1 and id in ($2);`

    rows, err := postgres.DB().Query(query, owner, pq.Int64Array(ids))
    if err != nil {
        return nil, err
    }
    defer rows.Close()
    return formRowsToList(rows)
}
Expected
      <*pq.Error | 0xc42033d9e0>: {
          Severity: "ERROR",
          Code: "22P02",
          Message: "invalid input syntax for integer: \"{354,355}\"",
          Detail: "",
          Hint: "",
          Position: "",
          InternalPosition: "",
          InternalQuery: "",
          Where: "",
          Schema: "",
          Table: "",
          Column: "",
          DataTypeName: "",
          Constraint: "",
          File: "int8.c",
          Line: "99",
          Routine: "scanint8",
      }
  to be nil

If I replace pq.Int64Array with pq.Array I still got the problem.

Also the request appears to be correct when I write it in an pg shell :

select * from forms_data where owner_id = 165 and id in (354,355);

Also I just did a pull from the master.

Thank you for your help and the great work.

@cbandy
Copy link
Contributor

cbandy commented Oct 8, 2016

IN (...) is an SQL construct that takes more than one literal. Each placeholder/parameter in a query is a single literal.

You can use the ANY() array function to write equivalent SQL that works with a single array literal: id = ANY($2).

@fdelbos
Copy link
Author

fdelbos commented Oct 8, 2016

Thanks cbandy, it works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants