SearchLight.jl icon indicating copy to clipboard operation
SearchLight.jl copied to clipboard

Date and DateTimes in DB Schemas

Open Akaame opened this issue 6 years ago • 3 comments

Hi,

I am fairly new to Julia and I have started out using Genie Framework just today.

I created this model with a Date field in it:

` using SearchLight using Dates

mutable struct Customer <: AbstractModel
_table_name::String
_id::String
_serializable::Vector{Symbol}

Name::String
CreateTime::Date
id::DbId
end

`

It is correctly saved to SQLite as type of "DATE". However, when I try to read the record it fails to convert String to type Date at the line below. Conversion line

convert(Dates.Date, "2019-09-01") throws the same exception. I could not find a workaround for this on the Internet. What is the correct approach for developers to work with Date types?

Akaame avatar Aug 31 '19 08:08 Akaame

@Akaame It's hard to tell without the full error stack trace and the output of julia> versioninfo() and pkg> st. It would help if you can share that when opening any issue with any Julia packages.

My guess is, Julia/Genie/SearchLight doesn't know how to convert the String to Date.

In order to convert a string to a date you need parse:

parse(Date, "2019-09-01")
2019-09-01

Add this to config/initializers/converters.jl:

using Dates
convert(::Type{Date}, s::String) = parse(Date, s)

Then restart the app, it should work.

essenciary avatar Aug 31 '19 10:08 essenciary

Thanks for your quick response. To my understanding, we are adding a convert overload to make String to Date conversion work. Are there any future plans to add this into the framework?

Thank you.

Akaame avatar Sep 01 '19 18:09 Akaame

I already did, it's on v0.17#master :) https://github.com/GenieFramework/Genie.jl/blob/master/files/new_app/config/initializers/converters.jl

essenciary avatar Sep 01 '19 18:09 essenciary