SQL Liquibase:如何将日期列的默认值设置为 UTC 格式的“现在”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23550232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Liquibase: How to set the default value of a date column to be "now" in UTC format?
提问by Jeff
How do you set the default value of a date column to be "now" in UTC format? I think the answer involves the defaultValueComputed
attribute on the column element.
如何将日期列的默认值设置为 UTC 格式的“现在”?我认为答案涉及列元素defaultValueComputed
上的属性。
The documentation states:
该文件指出:
defaultValueComputed A value that is returned from a function or procedure call. This attribute will contain the function to call.
defaultValueComputed 从函数或过程调用返回的值。此属性将包含要调用的函数。
What langauge is the function referred to supposed to be written in? Java? Is the function supposed to be the database vendor -specific date function I want to use? Is there any more documentation I can read on this topic?
所指的函数应该用什么语言编写?爪哇?该函数是否应该是我要使用的特定于数据库供应商的日期函数?我可以阅读有关此主题的更多文档吗?
回答by Jens
Maybe this topicin the liquibase forum will help?
也许liquibase 论坛中的这个主题会有所帮助?
I think defaultValueComputed
will take a database specific function to express "now". In mySQL it would be CURRENT_TIMESTAMP
so it could look like this:
我想defaultValueComputed
会用一个数据库特定的函数来表达“现在”。在 mySQL 中,CURRENT_TIMESTAMP
它可能看起来像这样:
<createTable tableName="D_UserSession">
<column name="ts" type="TIMESTAMP" defaultValueComputed="CURRENT_TIMESTAMP"/>
</createTable>
(Copied from the forum post.)
(复制自论坛帖子。)
回答by Pavel S.
In MySQL, to use a DATETIME column with fractions of second like DATETIME(6)
(microseconds precision), use default value of NOW(6)
(caution: CURRENT_TIMESTAMP(6)
for some reason produces an error with me using liquibase 3.5.3):
在 MySQL 中,要使用具有秒分数的 DATETIME 列DATETIME(6)
(微秒精度),请使用默认值NOW(6)
(注意:CURRENT_TIMESTAMP(6)
由于某些原因,我使用 liquibase 3.5.3 会产生错误):
<column name="created_at" type="DATETIME(6)" defaultValueComputed="NOW(6)" >
<constraints nullable="false" />
</column>
Note that the value will be stored internally in UTC, but read using the server's timezone settings (@@global.time_zone
, @@session.time_zone
).
请注意,该值将以 UTC 内部存储,但使用服务器的时区设置 ( @@global.time_zone
, @@session.time_zone
)读取。
回答by Neha
This works with SQlite:
这适用于 SQlite:
<column name="last_updated_at" type="TIMESTAMP" defaultValueComputed="CURRENT_TIMESTAMP">
<constraints nullable="false"/>
</column>
Adding '$now' didn't work for me. I am using SQlite as the DB.
添加“$now”对我不起作用。我使用 SQLite 作为数据库。
回答by Sagar Jani
As liquibase is common changelog for any database, to make it generic you should not depend on any specific database like oracle, postegres, mysql instead it should be generic enough to work for any/every database.
由于 liquibase 是任何数据库的常见变更日志,为了使其通用,您不应依赖于任何特定的数据库,如 oracle、postegres、mysql,而是应该足够通用以适用于任何/每个数据库。
Below is how it should be implemented :
以下是它应该如何实施:
<column name="time" type="${type.datetime}" defaultValueComputed="${column.datetime.defaultValue}"/>
This should work for all databases, for oracle, it inserts SYSTIMESTAMP as DATA_DEFAULT.
这应该适用于所有数据库,对于 oracle,它将 SYSTIMESTAMP 作为 DATA_DEFAULT 插入。
回答by Pedro Madrid
This worked for me:
这对我有用:
<property name="now" value="UNIX_TIMESTAMP()" dbms="mysql"/>
<column name="ts" type="timestamp" valueDate="${now}"/>
I found it thanks to this answer: https://stackoverflow.com/a/9100388/3107952
由于这个答案,我找到了它:https: //stackoverflow.com/a/9100388/3107952
回答by 0x5a4d
I used function the database vendor. For Oracle it`s a sysdate:
我使用了数据库供应商的函数。对于 Oracle,它是sysdate:
<column name="create_date" type="DATETIME" valueDate="sysdate" defaultValueComputed="sysdate" />