fsprojects / fsprojects/FSharp.Data.SqlClient

Sql Errors not bubbling up - System.TypeInitializationException

Open
#268 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
F#
Stars
206
Forks
66
PR merge metrics
No merged PRs in 30d

Description

Description

When executing two delete statements in the wrong order,
e.g. violating the foreing key constraint, so for example, deleting headers table before rows table,
the type provider does not bubble up the sql exception at runtime,
but gives instead a type initialization exception which does not allow to properly track the issue,
without executing the command separately.

Repro steps

Please provide the steps required to reproduce the problem

(* connectionString is one of SqlServer 2014, 
   whereas devConnectionString is of SqlServer 2008 *)

[<Literal>]
let deleteMovement =
   @"DELETE
    FROM Movements
    WHERE ID= @id
    "
type DeleteMovement = SqlCommandProvider<deleteMovement, connectionString>

let delMovement id =
     use cmd = new DeleteMovement(devConnectionString) 
     in printfn "%s" (cmd.ToTraceString id) ;
     cmd.Execute(id)

[<Literal>]
let deleteMovementRows =
   @"DELETE
    FROM MovementRows
    WHERE movement_ID = @MovementId
    "

type DeleteMovementRows = SqlCommandProvider<deleteMovementRows, connectionString>

let delMovementRows movementId =
    use cmd = new DeleteMovementRows(devConnectionString) 
    in printfn "%s" (cmd.ToTraceString movementId) ;
    cmd.Execute(movementId) 

let deleteMovementsAndRows ids =
    ids |> Seq.iter(fun x -> 
            delMovement x |> printfn "deleted movements %A";
            delMovementRows x |> printfn "deletes movements rows %A"
        )
Expected behavior

SqlCommandProvider should raise an exception at runtime,
containing the appropriate error with constraint violation on foreign key,
or whatever constraint violation happens.

Actual behavior

An unhandled exception of type System.TypeInitializationException occurred in [...].exe

Additional information: The type initializer for <StartupCode$MessageGenerator>.$Program threw an exception.

Known workarounds

No workarounds at this time.
just grab sql and execute it against database in the same order of execution, to get the correct error message that should be displayed

Related information
  • Windows 8 64 bit
  • latest stable release
  • Sql server 2008 for query execution, Sql server 2014 for type provider generation
  • .NET 4.6
  • Editing Tools - VS2015

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SqlCommandProvider repro and compare the generated type-provider behavior with runtime execution against SQL Server 2008 and 2014. Verify that a foreign-key constraint violation is surfaced as the SQL exception rather than System.TypeInitializationException; the issue provides no source file or test name.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.