Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For struct with nothing as default value, find converts non-NULL fields in the DB record to nothing #68

Open
PGimenez opened this issue Apr 3, 2023 · 0 comments

Comments

@PGimenez
Copy link
Member

PGimenez commented Apr 3, 2023

Full code for the MWE here: https://github.com/GenieFramework/CodeExamples/tree/main/src/3.database/null_values

I have a struct where one of its fields can be Intor Nothing

@kwdef mutable struct House <: AbstractModel
    id::DbId = DbId()
    street::String = "Random street"
    size::Float32 = 100.0
    rooms::Union{Int64,Nothing,Missing} = nothing
end

When running this to load two hoses, one with nothingin rooms and oner with 4

using SearchLight
using SearchLightSQLite
include("app/resources/houses/Houses.jl")
using .Houses
# Base.convert(t::Type{Nothing}, x::Int64) = x

SearchLight.Configuration.load() |> SearchLight.connect


h = House(street="Barcelona 2", size=400, rooms=nothing)
save(h)
h = House(street="Barcelona 3", size=400, rooms=4)
save(h)

@show find(House, SQLWhereExpression("street ==? AND size == ?", "Barcelona 2", 400))
@show find(House, SQLWhereExpression("street ==? AND size == ?", "Barcelona 3", 400))

the first one works, but the second one errors:

[ Info: SELECT "houses"."id" AS "houses_id", "houses"."street" AS "houses_street", "houses"."size" AS "houses_size", "houses"."rooms" AS "houses_
rooms" FROM "houses" WHERE street =='Barcelona 2' AND size == 400 ORDER BY houses.id ASC
find(House, SQLWhereExpression("street ==? AND size == ?", "Barcelona 2", 400)) = House[House
| KEY            | VALUE       |
|----------------|-------------|
| id::DbId       | 2           |
| rooms::Nothing | nothing     |
| size::Float32  | 400.0       |
| street::String | Barcelona 2 |
]
[ Info: SELECT "houses"."id" AS "houses_id", "houses"."street" AS "houses_street", "houses"."size" AS "houses_size", "houses"."rooms" AS "houses_
rooms" FROM "houses" WHERE street =='Barcelona 3' AND size == 400 ORDER BY houses.id ASC
┌ Error: MethodError(convert, (Nothing, 4), 0xffffffffffffffff)
└ @ SearchLight ~/.julia/packages/SearchLight/Ps2Js/src/SearchLight.jl:543
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Nothing
**Error stacktrace**

It appears that findconverts the field in the record to the type of the default value in the struct. One would expect it to convert to the other type in the Union when the record value is non-NULL.

I've done a quickfix by extending convert to return the record value when it is not NULL.

Base.convert(t::Type{Nothing}, x::Int64) = x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant