Code Monkey home page Code Monkey logo

Comments (5)

ahartman avatar ahartman commented on June 2, 2024

Dear Gwendal,
This morning my app crashes at startup, please find Crash Report attached.
If I take the code and run from Xcode, it will not crash.

This has to do with the same PatientTimeLine function that has the NULL value issues.
Hope this helps.
Regards, André Hartman
Crash Report.txt

from grdb.swift.

groue avatar groue commented on June 2, 2024

🎁 Thank you very much @ahartman,

This sample project and data indeed reveals a bug in GRDB. This bug prevents a normal error to be reported to your app. Next version will fix this.

Until then, I commented out the assertion, and the error is now correctly reported to your app (caught in DBModel.getPatientTimeline()):

could not decode Date from database value NULL
column: "minVisitVisitCreated"
column index: 2
row: [id:2289 patientName:"Aaliyah" minVisitVisitCreated:NULL maxVisitVisitDate:NULL]
sql: SELECT
       "patient"."id", "patient"."patientName",
       MIN("visit"."visitCreated") AS "minVisitVisitCreated",
       MAX("visit"."visitDate") AS "maxVisitVisitDate"
     FROM "patient"
     LEFT JOIN "visit"
       ON ("visit"."patientId" = "patient"."id")
       AND ("visit"."visitCalendar" IN (?, ?)) AND ("visit"."visitCalendar" IN (?, ?))
     GROUP BY "patient"."id"
     ORDER BY "patient"."patientName"
arguments: ["Marieke", "Marieke nieuwe", "Marieke", "Marieke nieuwe"]

So the database contains NULL in the minVisitVisitCreated column. This can happen when MIN("visit"."visitCreated") is NULL (if there is no filtered visit, for example).

This NULL value is decoded into PatientTimelineInfo, which has non-null dates:

struct PatientTimelineInfo: Decodable, FetchableRecord {
    var patient: PatientInfo.Patient
    var minVisitVisitCreated: Date
    var maxVisitVisitDate: Date
}

The fix (in your app) is to modify PatientTimelineInfo so that its dates are optional:

struct PatientTimelineInfo: Decodable, FetchableRecord {
    var patient: PatientInfo.Patient
    var minVisitVisitCreated: Date? // nil when there is no visit
    var maxVisitVisitDate: Date?    // nil when there is no visit
}

I'll take care of the bug in GRDB that prevented the correct error to be reported in the first place. 👍

from grdb.swift.

ahartman avatar ahartman commented on June 2, 2024

from grdb.swift.

groue avatar groue commented on June 2, 2024

Oh, yes, thanks for reminding me about this.

So I restored the .having(filteredVisits.isEmpty == false) line.

It runs correctly.

The SQL looks valid, if not ideal (note the repetition of the "visit"."visitCalendar" IN (?, ?) condition, which is superfluous):

SELECT
  "patient"."id", "patient"."patientName",
  MIN("visit"."visitCreated") AS "minVisitVisitCreated",
  MAX("visit"."visitDate") AS "maxVisitVisitDate"
FROM "patient"
LEFT JOIN "visit"
  ON ("visit"."patientId" = "patient"."id")
  AND ("visit"."visitCalendar" IN (?, ?))
  AND ("visit"."visitCalendar" IN (?, ?))
  AND ("visit"."visitCalendar" IN (?, ?))
GROUP BY "patient"."id"
HAVING COUNT(DISTINCT "visit"."rowid") > 0
ORDER BY "patient"."patientName"

Would you please remind me what prevents you from restoring the having clause? Now that I have a running app, it's easier to reproduce!

from grdb.swift.

ahartman avatar ahartman commented on June 2, 2024

from grdb.swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.