[英]Why have created_at and updated_at?
Many programming frameworks will automatically populate 2 fields to a model / table / collection.许多编程框架会自动将 2 个字段填充到模型/表/集合中。 In Rails the two fields are created_at and updated_at .
在 Rails 中,两个字段是created_at和updated_at 。 The function of the fields is self-explanatory.
字段的功能是不言自明的。
created_at created_at
The timestamp when the record was first created in the database.首次在数据库中创建记录时的时间戳。
updated_at更新时间
The timestamp of the most recent change to any of the data stored in the table.表中存储的任何数据的最近更改的时间戳。
I am working on a Meteor project which does not come out of the box with these fields, and so added createdAt and updatedAt in my project.我工作的一个流星计划不与这些领域来的开箱,所以加入createdAt和updatedAt在我的项目。
Then I began thinking: under what circumstances would we need to know when a record is first created, isn't the last updated enough?然后我开始思考:在什么情况下我们需要知道一个记录是什么时候第一次创建的,最后一次更新还不够吗?
user asked 10 mins ago
)user asked 10 mins ago
).. and more. .. 和更多。 Of course, it's possible that your record don't need it now, but it's always better to save this info, since you won't have a simple way to come up with it later if you don't save it now.
当然,您的记录现在可能不需要它,但保存这些信息总是更好的,因为如果您现在不保存它,以后将没有简单的方法来想出它。
I think there are a lot of situations where you'd want to know when something is first created.我认为在很多情况下,您都想知道某些东西是何时创建的。
Just taking stackoverflow as an example:仅以stackoverflow为例:
Knowing when a question was asked is important.知道何时提出问题很重要。 If you asked this question two years ago and then edited it yesterday it is very different from your having asked it yesterday.
如果你两年前问这个问题,然后昨天编辑它,这与你昨天问的非常不同。
Knowing when a question was answered is important.知道什么时候回答了问题很重要。 Think of some of the badges.
想想一些徽章。 "Curious: Ask a well-received question on 5 separate days, and maintain a positive question record.".
“好奇:在5个不同的日子里提出一个广受欢迎的问题,并保持积极的问题记录。”。 What if someone retags all my questions which triggers the
updated_at
to be set to today.如果有人重新标记了我所有的问题,从而触发了将
updated_at
设置为今天,该怎么办。 I've then gone from asking questions on different days to them appearing to be answered on the same day.然后,我从在不同的日子提出问题到他们似乎在同一天得到答复。
Those are just two examples.这只是两个例子。 The general answer is: whenever you want to know when an object first existed in your system you want
created_at
.一般的答案是:每当您想知道某个对象何时首次出现在您的系统中时,您都需要
created_at
。 Especially if you're then going to be updating that object.特别是如果您随后要更新该对象。
Not every table need those.不是每张桌子都需要那些。 If you don't have a clear use case, don't add those.
如果您没有明确的用例,请不要添加它们。 Your database and application will take additional performance hit from calculating, storing and reading them, for nothing.
您的数据库和应用程序将通过计算、存储和读取它们而导致额外的性能损失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.