vapor / sqlite-kit

Non-blocking SQLite client library with SQL builder built on SwiftNIO
MIT License
59 stars 30 forks source link

Support date without time #78

Open Rubenfer opened 4 years ago

Rubenfer commented 4 years ago

Vapor 4 cannot decode dates.

Steps to reproduce

  1. Create an empty project using vapor-beta toolbox and using a SQLite database for testing purposes.

  2. Open Todo.swift file and add a field to store a date

Todo model file looks:

import Fluent
import Vapor

final class Todo: Model, Content {
    static let schema = "todos"

    @ID(key: .id)
    var id: UUID?

    @Field(key: "title")
    var title: String

    @Field(key: "date")
    var date: Date

    init() { }

    init(id: UUID? = nil, title: String, date: Date) {
        self.id = id
        self.title = title
        self.date = date
    }
}
  1. Configure the field on the migration file.
import Fluent

struct CreateTodo: Migration {
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("todos")
            .id()
            .field("title", .string, .required)
            .field("date", .date, .required)
            .create()
    }

    func revert(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("todos").delete()
    }
}
  1. Run the migrations.

  2. Create a new Todo with ISO8601 date.

  3. Try to get the todos. It fails.

Expected behavior

The list of todos is returned.

Actual behavior

[ ERROR ] invalid field: date type: Date error: Decoding error: Value of type 'Date' required for key ''.

Environment

0xTim commented 4 years ago

Looks like an SQLite error, this works fine with Postgres

Rubenfer commented 4 years ago

Yes, I have other projects with Postgres and it works, but today I've tried in a SQLite project just for a quick test and it fails. This is not a critial bug for productions environmets but good to know.

0xTim commented 4 years ago

As a temporary workaround, if you change the migration type to .datetime, this then works

tanner0101 commented 4 years ago

SQLite currently only supports storing Date in the .datetime data type. I've updated this issue to be a feature request for date without time support.