蒸气MySQL时间戳默认值

时间:2018-10-26 22:55:47

标签: mysql swift xcode vapor

我正在尝试创建一个具有各种时间戳(默认值createdAt,updatedAt,deletedAt)的模型。如何做到这一点?我尝试使用prepare方法,但无济于事。预先谢谢你。

这是我目前的课程: enter image description here

1 个答案:

答案 0 :(得分:0)

蒸气提供类似的东西:

 static func prepare(on connection: MySQLConnection) -> Future<Void> {
        return Database.create(self, on: connection) { builder in
             builder.field(for: \.iq, type: .int, .default(.literal("123456")))
            ...

这也应该与日期一起使用。

或者您可以尝试在自定义init中设置它们:

 required init(id:Int?, adressLine1:String, adressLine2:String?, city:String, state:String, zip:String) {
    self.id = id
    self.adressLine1 = adressLine1
    self.adressLine2 = adressLine2
    self.city:String = city:String
    self.state = state
    self.zip = zip
    self.createdAt = Date() //<---- here
    self.updatedAt = Date() //<---- here 
    self.deletedAt = Date() //<---- here
}

或:

required init(id:Int?, adressLine1:String, adressLine2:String?, city:String, state:String, zip:String, 
createdAt:Date = Date()/*<--- here*/, updatedAt:Date = Date()/*<--- here*/,deletedAt:Date = Date()/*<--- here*/) {
        self.id = id
        self.adressLine1 = adressLine1
        self.adressLine2 = adressLine2
        self.city:String = city:String
        self.state = state
        self.zip = zip
        self.createdAt = createdAt
        self.updatedAt = updatedAt
        self.deletedAt = deletedAt
    }