marten icon indicating copy to clipboard operation
marten copied to clipboard

LINQ expression value not set in Npgsql command

Open MeikelLP opened this issue 2 months ago • 1 comments

The following query

[StronglyTypedId(Template.Guid)]
public readonly partial struct ShopId;

var shopId = new ShopId(Guid.Parse("00000005-0000-0000-0000-000000000000"));

await _session.Query<Product>().Where(x => x.Shops.Any(s => s.ShopId== shopId)).ToPagedListAsync(pageNumber, pageSize, token);
// using strongly typed ID value
await _session.Query<Product>().Where(x => x.Shops.Any(s => s.ShopId.Value == shopId.Value)).ToPagedListAsync(pageNumber, pageSize, token);

will ignore the where condition or rather build wrong SQL:

-- $2 = 50
 select d.id, d.data, d.last_modified_by, d.mt_version, count(*) OVER() as total_rows from my_db.mt_doc_product as d where d.data -> 'Shops' @> $1 order by d.data ->> 'Name' LIMIT $2

Expected

$1 should be '[{"ShopId": "00000005-0000-0000-0000-000000000000"}]'

Actual

$1 = '[{}]'

From debugging I assume it's because _data is not set

Image

I think it's because the query can't handle strongly typed IDs? I added my value types to marten via

options.RegisterValueType<ShopId>();

Workaround

Use raw SQL

MeikelLP avatar Oct 21 '25 13:10 MeikelLP

@MeikelLP The strong typed identifier stuff makes everything harder. In this case you would want Marten to "know" to use the value type as really the raw inner type. So that exact permutation of the LINQ support needs to be looking for strong typed identifiers.

And with those damn things, always use this: s => s.ShopId== shopId and not the .Value check. All of this is hard because Marten has to constantly treat value types as "special" because the strong typed identifiers are persisted as the inner, primitive value in the JSON

And this:

"Might be adding a new official rule that you have to first contribute to one of the projects or sponsor JasperFx before you're allowed to complain about anything new with strong typed identifiers"

jeremydmiller avatar Oct 22 '25 12:10 jeremydmiller