vapor / fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
MIT License
149 stars 53 forks source link

Type date but expression is of type text (transformAssignedExpr) #164

Closed amine2233 closed 4 years ago

amine2233 commented 4 years ago

after a decision to change the database from sqlite to Postgres I have this issue, all the code work fine with sqlite but when I use Postgres to create a user for example I have this issue

[ ERROR ] column "created_at" is of type date but expression is of type text (transformAssignedExpr)
[ ERROR ] server: column "created_at" is of type date but expression is of type text (transformAssignedExpr) [request-id: 523EB5D2-78F6-4704-81D9-4562B810C45B]

my user model

import Fluent
import Vapor

final class User: Model, Content {
    static let schema = "users"

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

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

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

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

    // When this Planet was created.
    @Timestamp(key: "created_at", on: .create, format: .iso8601)
    var createdAt: Date?

    // When this Planet was last updated.
    @Timestamp(key: "updated_at", on: .update, format: .iso8601)
    var updatedAt: Date?

    @Children(for: \.$user)
    var devices: [Device]

    @Children(for: \.$user)
    var tokens: [Token]

    struct FieldKeys {
        static var email: FieldKey { "email" }
        static var name: FieldKey { "name" }
        static var code: FieldKey { "code" }
        static var createdAt: FieldKey { "created_at" }
        static var updatedAt: FieldKey { "updated_at" }
        static var device: FieldKey { "device_id" }
    }

    init() {}

    init(id: UUID? = nil, email: String, name: String, code: String) {
        self.id = id
        self.email = email
        self.name = name
        self.code = code
    }
}
amine2233 commented 4 years ago

Finally the Postgres don't like specific format on @Timestamp

tanner0101 commented 4 years ago

In case someone else gets this error, as docs mention .iso8601 must be stored in a .string field: https://docs.vapor.codes/4.0/fluent/model/#timestamp-format

tanner0101 commented 4 years ago

@grundoon That part is all correct, it's just that you need this in your migration:

.field("updated_at", .string)

The documentation should probably make that more explicit TBH. You have to be paying pretty close attention to notice the "type = String" column and also understand the implications of that.

grundoon commented 4 years ago

@tanner0101 Yeah, I immediately noticed my error, edited the comment, then deleted it (sure wish GH would send out the edits). 🙃

grundoon commented 4 years ago

Quickie PR here.