mysql - Vapor MySQL 时间戳默认值

标签 mysql swift xcode vapor

我正在尝试创建一个具有各种时间戳(createdAt、updatedAt、deletedAt)和默认值的模型。如何实现?我尝试使用 prepare 方法,但无济于事。提前谢谢你。

这是我现在的类(class): enter image description here

最佳答案

Vapor 提供以下内容:

 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")))
            ...

这也适用于日期。

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

 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
    }

关于mysql - Vapor MySQL 时间戳默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53017227/

相关文章:

mysql - 分区表,每个分区位于我的硬盘上的不同磁盘上

swift - 如何在 Swift 中为 `dispatch_function_t` 声明 `dispatch_async_f`?

ios - 如何快速显示来自API的图像?

iOS-将图像制作为视频时,CVPixelBufferCreate 内存无法正确释放

xcode - 标签栏按钮出现问题,标签栏按钮变得不可点击

mysql - MySQL中的case语句

php - mysql_connect 连接生命周期

MySQL正则表达式更新查询

ios - 如何在 Xcode 中调整场景大小?

ios - 如何禁用导航栏动画?