- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个与 sqlite3
通信的应用程序使用 GORM ORM 的数据库。现在问题是我需要制作 UpdatedAt
列一个unix时间戳,因为另一个遗留应用程序正在使用相同的数据库并且它们应该是兼容的。
所以我尝试更新 UpdatedAt
BeforeUpdate
上带有 unix 时间戳的字段使用以下代码块 Hook 。
func (c *CartItems) BeforeUpdate() (err error) {
fmt.Println("----------------------------------------")
fmt.Println("BeforeUpdate")
fmt.Println( c )
c.UpdatedAt = time.Now().Unix()
fmt.Println("----------------------------------------")
return
}
----------------------------------------
BeforeUpdate
&{55 21 4 7 1585607114 1585607114 {0 [] [] [] {0 0 } 0 0} {0 0 0 0}}
----------------------------------------
[2020-03-31 04:30:02] [0.46ms] UPDATE "cart_items" SET "quantity" = 7, "updated_at" = '2020-03-31 04:30:02' WHERE "cart_items"."id" = 55
[1 rows affected or returned ]
[GIN] 2020/03/31 - 04:30:02 | 200 | 97.963597ms | 127.0.0.1 | POST "/cache/cart/21/item"
最佳答案
如果您想拥有 UpdatedAt()
,请使用以下命令和 CreatedAt()
作为unix时间戳。
type CartItems struct {
CreatedAt int64
UpdatedAt int64
}
func (m *CartItems) BeforeUpdate(scope *gorm.Scope) error {
scope.SetColumn("UpdatedAt", time.Now().Unix())
return nil
}
func (m *CartItems) BeforeCreate(scope *gorm.Scope) error {
if m.UpdatedAt == 0 {
scope.SetColumn("UpdatedAt", time.Now().Unix())
}
scope.SetColumn("CreatedAt", time.Now().Unix())
return nil
}
gorm
没有很好的文档记录,因此您必须阅读代码以了解其工作原理,即
this线路调用上述
BeforeUpdate()
功能。
callMethod()
中看到的那样函数,它检查
switch
中函数的签名决定如何调用该函数。
关于go - 在 gorm 中为 UpdatedAt 字段使用 unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60941097/
我想确保某个doc_type的所有文档都具有一个“updatedAt”时间戳(ISO 8601),该时间戳将在文档更新时进行更新。它需要是服务器端的时间戳,因为我不知道我是否可以相信所有客户端时间都是
我想弄清楚如何将日期“updatedAt”从 parse.com 转换为我可以更改的日期,例如 2014 年 9 月。 3. 我在 parse.com 上的文档中没有找到对此的任何解释。 这是我从 P
我在更新时更新记录有问题,而 415 是 id。 Cannot create property 'updatedAt' on string '415' 我还在模型中添加了更新字段,但仍然出现错误 最佳
下面是我在 Hooks 中用于更新两个对象的 updatedAt 列的代码: hooks: { afterUpdate: (group, options, callba
当我将模型存储在我的 MySQL 数据库中时,它们是不可变的。结果,我可以看到我的表中需要 createdAt 列,但我不需要多余的 updatedAt 列。我可以配置 sequelize 不存储 u
在发送 put 时请求,dailystatus仅列会使用数据进行更新,但 updatedAt列被留下而不更新最新的日期/时间。在 post 过程中,日期和时间自动插入 createdAt 和 upda
我有一个使用 Sequelize 作为 ORM 的快速应用程序。基本上,列 createdAt 和 updatedAt 由 Sequelize 自动生成。 每次更新行中除updatedAt 列之外的数
我使用 sequelize 创建了一个带有时间戳的表。当我更新表时,它会自动更新时间戳(即 createdAt 和 updatedAt)。但这些时间与我的本地时间不同。如果我使用 moment 像这样
我想通过两种方式保存updatedAt。 1) 如果我在保存数据时在该对象中传递了一些值,那么它应该在该日期之前保存,否则是当前日期和时间。 即今天当前日期是 2018-07-27 16:41:14
如何根据 updatedAt 列进行搜索。我希望所有记录在 date 之后按升序排列。 var date = "2016-06-18 05:18:27" Person.findAll({
我在 ExpressJS 应用程序中使用 SequlizeJS ORM 与 MariaDB 进行通信。我正在使用现有的数据库方案,因此无法更改字段的数据类型。现有数据库在createdAt、updat
我正在使用 Node 14 和 Sequelize 对现有的 Postgres 数据库进行建模。 已经创建的表格没有 createdAt neighter updatedAt Colums 所以我想指
我有一个带有 nodeJS 问题的 sequelize V5。 我有几个模型,用户,产品,并且想将 createdAt 和 updatedAt 列添加到我的 MYSQL 数据库中的这些表中。 现在我不
我需要禁用字段updatedAt。我现在,有一个解决方案 timestamps: false, 进入模型,但它也会禁用字段 createdAt,这是必要的。 最佳答案 您可以设置 timestamps
嘿,我对 SEQUELIZE 真的很陌生,对于这个项目,我试图在转换为秒后扣除 updatedAt 和 createdAt 值。 Updated 和 createdAt 列采用 DATETIME 格式
在 Symfony2 项目中,我有一个 Doctrine 实体,它有一个名为 lastAccessed 的 datetime 字段。此外,该实体使用 Timestampable在 updatedAt
我有兴趣知道如何在使用java同步驱动程序时在mongo上拥有createdAt和updatedAt字段。 我知道如何在 Mongoose 中做到这一点 const SchemaName = new
我正在使用 Knex.js(0.19.0) 和 PostgreSQL(11-alpine, pg@7.11.0 做一个 REST 教程>),并且我注意到当我发出 PUT 请求并更新数据时,update
我正在努力整合以下框架,以自动将 JSON 响应映射到 Realm 并将对象保存到数据库。 Realm 阿拉莫菲尔 对象映射器 AlamofireObjectMapper 如果它是一个新对象,我想设置
我正在将数据从 MS SQL 迁移到 MongoDB。我正在使用 Mongoose ,并在我的架构中将时间戳属性设置为 true。 { timestamps: true } 然后,我尝试设置cre
我是一名优秀的程序员,十分优秀!