Litedb: [BUG] Adding an immutable record to LiteDB gives an NRE

Created on 7 Mar 2020  路  6Comments  路  Source: mbdavid/LiteDB

Version: LiteDB 5.0.3, Windows, NetCore3.1

Describe the bug
Adding an immutable record to LiteDB gives an NRE, although the document is actually inserted.

Code to Reproduce

open LiteDB

type Customer = {
    Id : int
    Name : string
    Phones : string array
    IsActive : bool
}

let db = new LiteDatabase("MyData.db")
let col = db.GetCollection "customers"

let customer =
    { Id = 0
      Name = "John Doe"
      Phones = [| "8000-0000"; "9000-0000" |]
      IsActive = true }

// Insert new customer document (Id will be auto-incremented)
col.Insert customer |> ignore

Expected behavior
The record should be inserted without an exception.

System.NullReferenceException: Object reference not set to an instance of an object. at LiteDB.LiteCollection1.Insert(T entity)`

Additional context
You can work around this by marking the record type with [<CLIMutable>]. However, this is non-idomatic from an F# perspective.

bug

All 6 comments

FWIW https://github.com/Zaid-Ajaj/LiteDB.FSharp#id-auto-incremented

So this might be by design (cannot tell for sure) but the error should be more specific then.

Yes, maybe that's all that needs to be done. Note that LiteDB.FSharp is not compatible with v5 of LiteDB cc @Zaid-Ajaj

There is already an opened issue about updating LiteDB.FSharp to latest LiteDB version. I won't be able to work on it any time soon due to other prioritized projects. Feel free to send PRs for the issue in subject, I will be happy to review them

You can work around this by marking the record type with [<CLIMutable>]. However, this is non-idomatic from an F# perspective.

In LiteDB 4.x
Another workaround is to change int to ObjectId without adding [<CLIMutable>] attribute



///using `ObjectId.NewObjectId()` but not `new ObjectId()`

type Customer = {
    Id : ObjectId
    Name : string
    Phones : string array
    IsActive : bool
}

A issue is the record type using ObjectId will not support comparision
image

@humhei This happens because ObjectId implements IComparable<ObjectId>, but not IComparable (an oversight, I believe). Try using Guid as Id, it should work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muhamad picture muhamad  路  3Comments

sandolkakos picture sandolkakos  路  3Comments

RealBlazeIt picture RealBlazeIt  路  3Comments

darcome picture darcome  路  3Comments

darcome picture darcome  路  3Comments