Search code examples
f#dapper

Dapper F# - A Parameterless default A parameterless default constructor or one matching signature


I'm making, for demo purposes, an Insert function that I would like to return a variable of the type that is passed in.

I'm user Dapper.fsharp for the initial query, then I'd like to run a raw SQL query to get the last inserted value.

So I have something like this for demo purposes

let Insert<'a> asyncQuery =
    asyncQuery
        |> connection.InsertAsync<'a>
        |> RunSynchronously
        |> ignore
    (*This is a Dapper.fsharp query. Running this returns the number of rows inserted (int) *)

    let table = asyncQuery.Table (*This is a string*)
    let result = 
          connection.Query<'a>($"""Select * From {table} Where id = (select last_insert_id())""") (*Should return an IENumerable of type 'a*)
          |> EnumerableToArray
           
    result |> first

And then that's called like

let newSession =
            insert {
                table "sessions"
                value newSession
            } |> Insert

where newSession is of type session

module Session
type session = {id: int; session_id: string; clerk_json: string; clerk_id: int; expires: int}
(*This is also the structure of the SQL table exactly*)

The error I get is

"A parameterless default constructor or one matching signature (System.Int32 id, System.String session_id, System.Int32 clerk_id, System.String clerk_json, System.Int32 expires) is required for Session+session materialization"

Which indicates to me that it's not getting the right type signature from the database, but the column names and type match, and nothing in the table is null.

Maybe I'm overlooking something simple or misunderstanding how the library should be used?


Solution

  • The type has to be [<CLIMutable>]...