【发布时间】:2020-04-09 06:49:03
【问题描述】:
我的后端使用 Vapor 3、Swift 5.1、PostgreSQL 12 和 Postico 1.5.10。在 PostgreSQL 数据库中,有一个名称为 eventDate 的字段,类型为 timestamp without time zone。
请看截图:
在 Vapor 中,有一个模型:
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 {
}
但是当我试图将Date (Swift) 保存到timestamp without time zone 时,Vapor 给了我错误:
[ ERROR ] DecodingError.typeMismatch: Value of type 'String' required for key 'eventDate.date'. (NIOServer.swift:104)
如果在模型中,我将 Date 更改为 String,Vapor 会给我这个错误:
[ ERROR ] PostgreSQLError.server.error.transformAssignedExpr: column "eventDate" is of type timestamp without time zone but expression is of type text (NIOServer.swift:104)
那么问题来了:如何将Date和String转移到timestamp without time zone(或timestamp with time zone)?
我将不胜感激任何帮助或建议!
【问题讨论】:
标签: swift xcode postgresql vapor postico