Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Is there any way to make this work?
    My query is something like this:
    SELECT * FROM items WHERE account_id = $1 AND lower(name) LIKE '%$2%'
    I provide $1 and $2 but it gives me the error
    pq: got 2 parameters but the statement requires 1
    I know if I change that to = $2 it works
    no complaints about "got 2 parameters, requires 1"
    I've tried escaping this multiple ways, but it doesn't work. How are you supposed to use "Like" statements?
    Thanks in advance.
    If I go completely around this and build the string manually, it completely works, but that's VERY prone to SQL injection.
    Samuel Nelson
    like $2 will work, you just need to add the '%' characters to the string
    also, i really hope you're not actually doing SELECT * in your code
    I tried it hence asking here
    (sorry was typing with one hand as was on phone to boss :)
    The query has been slightly changed so as to preserve company IP
    but the point is ONE or the OTHER will work
    where account_id = $1
    where name like $2 where $2 is "'%" + foo + "%'"
    But I need BOTH to work because I need to do the above query
    How do I do the above query in GORP where it will work?
    That same query done directly to Postgres gives results
    whereas with Gorp it does not
    Samuel Nelson
    what I said, but you don't use the quotes in $2
    Oh hold on, so where $2 is "%" + foo + "%"
    let me try that
    Samuel Nelson
    yep, that one should work
    Oh, that's perfect!
    Let me find the docs and try and add that to them... I'm sure that'll help someone down the road.
    Thanks for your quick and frankly really awesome reply.
    Samuel Nelson
    no problem :)
    little more technical detail: when postgres does the variable binding, it will figure out the correct type for the value; when it realizes the value is a string, it will effectively add the quotes for you
    so having the quotes around it made postgres bind that as if you were doing like '\'%foo%\''

    Hi, I'm having problems with the primarykey tag in gorp. It seems to be ignored resep. used as column name !?

    type Meta struct {
        Id        string    `db:"uuid, primarykey"`
        CreatedAt time.Time `db:"createdAt"`

    That ends up in the database as:

     \d gtin
                                 Table "public.gtin"
          Column      |           Type           | Collation | Nullable | Default 
     uuid, primarykey | text                     |           |          |
    .SetKeys works, but I'd like the tag much more :/
    DB is a postgres.
    I import gorp "gopkg.in/gorp.v1"
    ... and it works with v2. Is that mentioned in https://github.com/go-gorp/gorp ?
    Uh, December 2018 ... seems to be a very active project ;)
    Samuel Nelson
    ugh, mornings
    alright, so the docs and the readme were updated in master when that feature was added
    which means that if you're looking at the docs or the readme from the v1 version, they won't have that feature
    you have to be looking at master or v2 to see the docs about it
    also, per activity of the project: I merge PRs and I try to keep it caught up with the world, but the other maintainers dropped off the radar and I don't use SQL in my job any more
    so I kind of consider gorp to be a project that is "done" for the most part
    I want to ignore a field, but I need to join to query the field. What should I do?
    because this only saves the ID in another table in this table, I need the join query name, so the field representing the ID associated name in the structure is ignored.