Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Select in with uuid array not working in Postgresql

Open jhuggart opened this issue 10 years ago • 8 comments

I'm running a query similar to the following:

string sql = @"select * from table where table.token in (@tokens)";

using (var connection = connection_factory.OpenDbConnection())
{
    return connection
      .Query<Dto>(sql, new { tokens = new[] { Guid.Parse("d767cfb5-ad1f-4402-8147-db884873e9fe") });
}

It produces this sql:

select * from table
where table.token in ((('{"d767cfb5-ad1f-4402-8147-db884873e9fe"}')::uuid[]))

Which throws this error:

ERROR:  operator does not exist: uuid = uuid[] at character 164
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Am I doing something explicitly wrong? Is there a workaround for in statements containing uuids?

jhuggart avatar Jul 13 '15 16:07 jhuggart

In fact, Postgresql dose not receive ARRAY type when you use IN keyword.

You can use this: (ANY statement does receive array)

table.token = ANY(@tokens)

Or you can use ToString() of Guid type, like this:

var tokens = (new[] { Guid.Parse("d767cfb5-ad1f-4402-8147-db884873e9fe") }).Select(x => x.ToString());

var tokenInString = “‘“ + tokens.Join(“‘,'”, tokens) + “'";

return connection.Query<Dto>(@"select * from table where table.token in (“ + tokenInString + “)”);

在 2015年7月14日,上午12:06,jhuggart [email protected] 写道:

I'm running a query similar to the following:

string sql = @"select * from table where table.token in (@tokens)";

using (var connection = connection_factory.OpenDbConnection()) { return connection .Query<Dto>(sql, new { tokens = new[] { Guid.Parse("d767cfb5-ad1f-4402-8147-db884873e9fe") }); } It produces this sql:

select * from table where table.token in ((('{"d767cfb5-ad1f-4402-8147-db884873e9fe"}')::uuid[])) Which throws this error:

ERROR: operator does not exist: uuid = uuid[] at character 164 HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. Am I doing something explicitly wrong? Is there a workaround for in statements containing uuids?

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/dapper-dot-net/issues/315.

ceeji avatar Aug 10 '15 07:08 ceeji

So I know what to test, is this using the v2 or v3 npg driver? I realize we'll need to look at syntax changes based on provider here (always fun!), but I'm assuming v3 is what we're test against going forward...and want to confirm.

NickCraver avatar Nov 28 '15 14:11 NickCraver

@NickCraver I am using 3.0.3

jhuggart avatar Dec 09 '15 14:12 jhuggart

@NickCraver I can reproduce this issue in 3.1.9

danielmahadi avatar Jan 03 '17 06:01 danielmahadi

@NickCraver I'm hitting the same with Npgsql 3.1.9 and Dapper 1.50.2 in .NET Core. The only solution was to use the column = ANY(@array) syntax, which could be better in terms of query caching than generating a different query depending on the number of parameters and I guess indexes still work under these scenarios.

arturosevilla avatar Jan 09 '17 09:01 arturosevilla

@NickCraver I have same problem with Npgsql 4.0.6 and Dapper 1.60.6 in .NET Core.

tuannguyendotme avatar May 13 '19 08:05 tuannguyendotme

I still have the same problem Dapper 2.0.30 .Net 6

pouryadel avatar Dec 07 '21 11:12 pouryadel