1

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:

enter image description here

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!

xTwisteDx
  • 2,152
  • 1
  • 9
  • 25
Alex
  • 1,038
  • 2
  • 12
  • 32

2 Answers2

0

why do you save time stamps without a time zone? time stamp without a time zone is not a date - it is a string.

any way - you just have to init a time format without a time zone, then just use it to get a string.

let formatter = DateFormatter()
// initially set the format based on your datepicker date / server String
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let myString = formatter.string(from: Date()) 
Alex
  • 159
  • 1
  • 9
  • Hello, thanks for help, but this is not working. If I am trying to save String (like yours format) in without time zone, I get error: column "date" is of type timestamp without time zone but expression is of type text – Alex Dec 17 '19 at 06:55
  • I am not using swift on server side - only client side. so i cannot give you a sample code - but i fond a link with explanation for you - and it tells you in more details what i told you just before. https://razvan.net/2017/02/23/swift-on-server-part-3/ – Alex Dec 19 '19 at 17:59
  • Thank you for this site, but method, he is using, it does not work. He uses Vapor 2 and some Nodes. I think, he just transfer Date to String with some special rules, but it does not work. Now rules changed, I think. – Alex Dec 26 '19 at 08:13
0

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 {
}
Alex
  • 1,038
  • 2
  • 12
  • 32