Dzoukr / Dapper.FSharp

Lightweight F# extension for StackOverflow Dapper with support for MSSQL, MySQL, PostgreSQL, and SQLite
MIT License
374 stars 35 forks source link

Can't query table with optional key #27

Closed tforkmann closed 3 years ago

tforkmann commented 3 years ago

Hi guys,

I'm trying to query following statement.

let enmsSichtKstEnergiezentrale = table<EnmsSicht_KstEnergiezentrale>
let enmsSichtKstErzeuger = table<EnmsSicht_KstErzeuger>
let! plants =
                select {
                    for ez in enmsSichtKstEnergiezentrale do
                    join erz in enmsSichtKstErzeuger on (ez.KstStelle = erz.USER_KstUebergeordnet.Value)
                    orderBy ez.KstStelle
                }
                |> conn.SelectAsync<EnmsSicht_KstEnergiezentrale,EnmsSicht_KstErzeuger>

Sadly I run into following error: Could not get data "The given key was not present in the dictionary."

I have to join on a optional key. In this case erz.USER_KstUebergeordnet.Value.

Do you guys have an idea what could be the reason for that error message?

Thanks,

Tim

JordanMarr commented 3 years ago

It looks like you are joining on an Option property. Try this: on (Some ez.KstStelle = erz.USER_KstUebergeordnet)

tforkmann commented 3 years ago

I now get following error message:

Could not get data "The method or operation is not implemented.

tforkmann commented 3 years ago

I guess that error message comes from here: https://github.com/Dzoukr/Dapper.FSharp/blob/aa89bd0f311d177472e61f9ad1c1db21475f1fc4/src/Dapper.FSharp/LinqExpressionVisitors.fs#L7

JordanMarr commented 3 years ago

Ah ok, you found a bug then. the bug is that Option types are only unwrapped in the where statement expressions. It should be a pretty easy fix though.

JordanMarr commented 3 years ago

Incoming PR...

Dzoukr commented 3 years ago

Fixed in v2.0.1 thanks to @JordanMarr đź‘Ź

tforkmann commented 3 years ago

Ahh thanks. Just wanted to sent a PR in as well^^ But yours is even better

tforkmann commented 3 years ago

Awesome works! Thanks for the help!

JordanMarr commented 3 years ago

Ahh thanks. Just wanted to sent a PR in as well^^ But yours is even better

Well it just occurred to me that group by probably has the same problem, so feel free to a unit test for groupBy and groupByMany that takes an Option type.

JordanMarr commented 3 years ago

Actually there is no groupByMany, just groupBy which can take a tuple, and it does indeed need the same fix if you’re up for it. (Let me know).

Also the xml doc comment for groupBy is wrong on the select builder.

JordanMarr commented 3 years ago

FYI, I just checked and groupBy worked fine with option types (I added a new unit test to make sure). So no other fix is needed.