Reputation: 10345
In my PostgreSQLModel
vapor class I'm using a simple Migration
extension to create the database 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)
}
}
}
The problem I'm having is my Date
fields are being created as timestamp without time zone
whereas what I really want is just date
. How do I specify this? Do I have to skip the addProperties
and manually call addField
on the builder myself for each column?
Upvotes: 1
Views: 99
Reputation: 10345
Not sure if it's the best way, but I ended up doing this:
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 $0.type.type == .timestamp else { return $0 }
let defaultValue = $0.name == CodingKeys.created.rawValue ? "now()" : nil
let type = PostgreSQLColumn(type: .date, default: defaultValue)
return SchemaField<PostgreSQLDatabase>(name: $0.name, type: type, isOptional: $0.isOptional)
}
}
}
}
Upvotes: 2