I am using Vapor 3, Swift 5.1, PostgreSQL 12, and Postico 1.5.10 for my Backend. In the PostgreSQL database, there is a field with the name eventDate
of type timestamp without time zone
.
Please, look at screenshot:
In Vapor, there is a model:
import Vapor
import FluentPostgreSQL
final class TestModel: PostgreSQLModel {
var id: Int?
var eventDate: Date
init(eventDate: Date) {
self.eventDate = eventDate
}
}
extension TestModel: Content {
}
extension TestModel: Migration {
}
extension TestModel: Parameter {
}
But when I am trying to save Date
(Swift) to timestamp without time zone
Vapor gives me error:
[ ERROR ] DecodingError.typeMismatch: Value of type 'String' required for key 'eventDate.date'. (NIOServer.swift:104)
If, in model, I am changing Date
to String
, Vapor gives me this error:
[ ERROR ] PostgreSQLError.server.error.transformAssignedExpr: column "eventDate" is of type timestamp without time zone but expression is of type text (NIOServer.swift:104)
So the question is: how to transfer Date
and String
to timestamp without time zone
(or timestamp with time zone
)?
I will be thankful for any help or advice!
It's no 100% solution I wanted but it's also very good.
import Vapor
import FluentPostgreSQL
final class TestModel: PostgreSQLModel {
static var createdAtKey: TimestampKey? = \.createdAt
var id: Int?
var someValue: Int
var someOtherProprty: String
var createdAt: Date?
init(someValue: Int, someOtherProprty: String) {
self.someValue = someValue
self.someOtherProprty = someOtherProprty
}
}
extension TestModel: Content {
}
extension TestModel: Migration {
}
extension TestModel: Parameter {
}