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

Could not convert MySQL data to Date on time field #221

Open
eugeniobaglieri opened this issue Aug 29, 2023 · 1 comment
Open

Could not convert MySQL data to Date on time field #221

eugeniobaglieri opened this issue Aug 29, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@eugeniobaglieri
Copy link

eugeniobaglieri commented Aug 29, 2023

Hi, I'm trying to save the time giving a swift date object.
I'm not getting any error on save and the time is saved without errors.
But when I try to query this model I get this error:

invalid field: 'opening_time', type: Date, error: typeMismatch(Foundation.Date, Swift.DecodingError.Context(codingPath: [], debugDescription: "Could not convert MySQL data to Date: <MYSQL_TYPE_TIME>", underlyingError: nil)) [request-id: C9E79175-5642-4AF1-B362-5FFDF0F9F077] (App/ErrorMiddleware.swift:63)

I defined this model:

final class Club: Model {
    
    static var schema: String { "clubs" }
    
    @ID(key: .id)
    var id: UUID?
    
    @Field(key: "name")
    var name: String
    
    @Field(key: "contact")
    var contact: String
    
    @Field(key: "address")
    var address: String
    
    @Field(key: "latitude")
    var latitude: Double
    
    @Field(key: "longitude")
    var longitude: Double
    
    @Children(for: \.$club)
    var playgrounds: [Playground]
    
    @OptionalChild(for: \.$club)
    var user: User?
    
    @Field(key: "active")
    var active: Bool
    
    @Field(key: "opening_time")
    var openingTime: Date
    
    @Field(key: "closing_time")
    var closingTime: Date
    
    @Field(key: "time_slot_duration")
    var timeSlotDuration: Int
    
    @Timestamp(key: "created_at", on: .create)
    var createdAt: Date?
    
    @Timestamp(key: "updated_at", on: .update)
    var updatedAt: Date?
    
    init() {}
}

with this schema:

func prepare(on database: Database) async throws {
        try await database.schema(Club.schema)
            .id()
            .field("name", .string, .required)
            .field("contact", .string, .required)
            .field("address", .string, .required)
            .field("latitude", .double, .required)
            .field("longitude", .double, .required)
            .field("opening_time", .time, .required)
            .field("closing_time", .time, .required)
            .field("time_slot_duration", .int, .required)
            .field("active", .bool, .required, .sql(.default(true)))
            .field("created_at", .datetime, .required)
            .field("updated_at", .datetime)
            .create()
    }

On query i should obtain an object with populated openingTime and closingTime

Environment

  • Framework: 4.78.2
  • Foolbox: main (83d3503)
  • OS version: Ventura 13.5
@eugeniobaglieri eugeniobaglieri added the bug Something isn't working label Aug 29, 2023
@0xTim
Copy link
Member

0xTim commented Aug 29, 2023

Yeah the trouble is there is no good way to represent time in Swift (the same applies to date fields as well). I'm not sure what the best suggested solution to this is, but maybe @gwynne can advise.

Each time this comes up I'm of the mind we should create our own types for only date and only time but there are a lot of edge cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants