指定日期字段的类型

Specify type of Date field

在我的 PostgreSQLModel vapor class 中,我使用一个简单的 Migration 扩展来创建数据库 table。

extension Order: Migration {
    static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
        return Database.create(self, on: connection) { builder in
            try addProperties(to: builder)
            try builder.addReference(from: \.stationID, to: \Station.id)
        }
    }
}

我遇到的问题是我的 Date 字段被创建为 timestamp without time zone,而我真正想要的只是 date。我该如何指定?我是否必须跳过 addProperties 并为每一列手动调用构建器上的 addField

不确定这是否是最好的方法,但我最终这样做了:

extension Order: Migration {
    static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
        return Database.create(self, on: connection) { builder in
            try addProperties(to: builder)
            try builder.addReference(from: \.stationID, to: \Station.id)

            builder.schema.addFields = builder.schema.addFields.map {
                guard [=10=].type.type == .timestamp else { return [=10=] }

                let defaultValue = [=10=].name == CodingKeys.created.rawValue ? "now()" : nil
                let type = PostgreSQLColumn(type: .date, default: defaultValue)

                return SchemaField<PostgreSQLDatabase>(name: [=10=].name, type: type, isOptional: [=10=].isOptional)
            }
        }
    }
}