MySQL “dateAdded”的默认值无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9005766/
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
Invalid default value for 'dateAdded'
提问by Robin Van den Broeck
I got a stupid problem with SQL that I can't fix.
我遇到了一个我无法修复的愚蠢的 SQL 问题。
ALTER TABLE `news` ADD `dateAdded` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AUTO_INCREMENT , ADD PRIMARY KEY ( `dateAdded` )
Error:
错误:
(#1067)Invalid default value for 'dateAdded'
Can somebody help me?
有人可以帮助我吗?
回答by Marc B
CURRENT_TIMESTAMP
is only acceptable on TIMESTAMP
fields. DATETIME
fields must be left either with a null default value, or no default value at all - default values must be a constant value, not the result of an expression.
CURRENT_TIMESTAMP
仅在TIMESTAMP
字段上可接受。DATETIME
字段必须保留空默认值,或者根本没有默认值 - 默认值必须是常量值,而不是表达式的结果。
relevant docs: http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
相关文档:http: //dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
You can work around this by setting a post-insert trigger on the table to fill in a "now" value on any new records.
您可以通过在表上设置插入后触发器以在任何新记录上填充“现在”值来解决此问题。
回答by David Soussan
CURRENT_TIMESTAMP
is version specific and is now allowed for DATETIME
columns as of version 5.6.
CURRENT_TIMESTAMP
是特定于版本的,现在允许用于DATETIME
5.6 版的列。
See MySQL docs.
请参阅MySQL 文档。
回答by Torsten Ojaperv
Also do note when specifying DATETIME
as DATETIME(3)
or like on MySQL 5.7.x, you also have to add the same value for CURRENT_TIMESTAMP(3)
. If not it will keep throwing 'Invalid default value'.
还要注意在 MySQL 5.7.x 上指定DATETIME
asDATETIME(3)
或 like 时,您还必须为CURRENT_TIMESTAMP(3)
. 如果不是,它将不断抛出“无效的默认值”。
回答by Darshn
I had the same issue, following fix solved my problem.
我有同样的问题,以下修复解决了我的问题。
Select Type as 'TIMESTAMP'
DON'T ENTER ANYTHING IN LENGTH/VALUES FIELD. KEEP IT BLANK
Select CURRENT_TIMESTAMP as Default value.
选择类型为“时间戳”
不要在长度/值字段中输入任何内容。保持空白
选择 CURRENT_TIMESTAMP 作为默认值。
I am using MySQL ver 5.5.56
我使用的是 MySQL 5.5.56 版
回答by Abraham Tugalov
I have mysql version 5.6.27 on my LEMP and CURRENT_TIMESTAMP as default value works fine.
我的 LEMP 和 CURRENT_TIMESTAMP 上有 mysql 版本 5.6.27 作为默认值工作正常。
回答by u4960473
mysql version 5.5 set datetime default value as CURRENT_TIMESTAMP will be report error you can update to version 5.6 , it set datetime default value as CURRENT_TIMESTAMP
mysql version 5.5 将 datetime 默认值设置为 CURRENT_TIMESTAMP 会报错你可以更新到 version 5.6 将 datetime 默认值设置为 CURRENT_TIMESTAMP
回答by Hamid ER-REMLI
Change the type from datetime to timestamp and it will work! I had the same issue for mysql 5.5.56-MariaDB - MariaDB Server Hope it can help... sorry if depricated
将类型从日期时间更改为时间戳,它将起作用!我对 mysql 5.5.56-MariaDB - MariaDB 服务器有同样的问题希望它可以帮助...抱歉,如果被贬低