mysql时间戳默认值

idfiyjo8  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(408)

我正在尝试创建一个具有各种时间戳(createdat、updatedat、deletedat)和默认值的模型。如何做到这一点?我试着玩弄那辆车 prepare 方法,但无济于事。先谢谢你。
这是我现在的课:

rn0zuynd

rn0zuynd1#

蒸汽提供了类似于:

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
    }

相关问题