Skip to content

Support dynamic sized IN clauses #167

Closed

Description

@oralordos

You should add support for a query with a dynamically sized list of values for an IN clause in a WHERE segment. This would be useful for when you are using a dataloader to get data from the database. This is most useful for graphql servers.

See the large example on this page for a use-case: https://gqlgen.com/reference/dataloaders/

I am thinking something like:

-- name: GetPeopleMany :many
SELECT * FROM people
WHERE id IN ($);

would generate a go function that takes in a slice of values for the input. This is distinguished from just wanting a single value by only having the $ instead of $1.

Activity

  1. kyleconroy commented on Dec 13, 2019

    @kyleconroy
    Collaborator

    You're in luck! sqlc already supports this using ANY. The following SQL:

    -- name: GetPeopleMany :many
    SELECT * FROM people
    WHERE id = ANY($1::int[]);
    

    generate this method signature

    func (q *Queries) GetPeopleMany(ctx context.Context, ids []int) ([]People, error) {
      ...
    }
    
  2. markschmid commented on Nov 21, 2020

    @markschmid

    Any way for this to work with mysql as well? Tried ANY(?::int[]) but it reports a syntax error.

  3. SebastienMelki commented on Jul 25, 2022

    @SebastienMelki

    Any way for this to work with mysql as well? Tried ANY(?::int[]) but it reports a syntax error.

    @kyleconroy I have the same issue, should this work for mysql or is it only available in postgres?

  4. josharian commented on Jul 28, 2023

    @josharian
    Contributor

    Ditto, but sqlite.

  5. orisano commented on Jul 29, 2023

    @orisano
    Contributor
  6. iceteahh commented on Dec 27, 2023

    @iceteahh

    -- name: GetPeopleMany :many
    SELECT * FROM people
    WHERE id = ANY($1::int[]);

    This won't work with enum type

  7. mbesida commented on Apr 4, 2024

    @mbesida

    -- name: GetPeopleMany :many SELECT * FROM people WHERE id = ANY($1::int[]);

    This won't work with enum type

    At the moment(Apr 2024) it works with enum types. You just need to specify the enum type (e.g. ANY($1::MyEnum[]))

  8. psnehanshu commented on May 8, 2025

    @psnehanshu

    @mbesida This cause error:

    failed to encode args[n]: unable to encode []CustomEnumType{\"v1\"} into text format for unknown type (OID 1234): cannot find encode plan
    
  9. BijaySharma commented on May 29, 2025

    @BijaySharma

    I used it this way and it works like charm.

    -- name: GetPeopleMany :many
    SELECT * FROM people
    WHERE id IN (sqlc.slice('ids'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions