Invalid default value for 'timestamp' - mysql

i am getting error in my database. i am encountering invalid default value for timestamp.
here's my database:
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`color` varchar(10) DEFAULT '#00bcd4',
`icon` varchar(100) NOT NULL DEFAULT 'https://mymonas.com/forum/category_icon/ic_question.png'
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

I was having the same problem, I changed type from "datetime" to "timestamp" and It worked. I have mysql 5.5.52.
Mysql_error

I have the same issue in sql_mode.
Make query:
show variables like 'sql_mode' ;
You need to remove the "NO_ZERO_IN_DATE,NO_ZERO_DATE" from sql_mode.
SET sql_mode = '';

Use CURRENT_TIMESTAMP() instead CURRENT_TIMESTAMP
i.e.
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
Now() works as well

From the MySQL 5.5 manual:
"You cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column."
The changes in MYSQL 5.6.x that allow the functionality are documented here:
"As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table."
So, this means you are using an older version of mysql, either you can use datetime data type of upgrade your mysql version

Answered by #max Sherbakov worked but I think its risky,
if you execute SET sql_mode = ''; query.
Because if you or other users SET any different variables in sql_mode
like NO_ENGINE_SUBSTITUTION check other SQL MODES
by changing sql_mode values in my.ini file
OR
using SET sql_mode = 'YOUR_VARIABLE_LIST'; query
it worked for you current situation
but create problem in other projects.
To view current sql mode use following query
show variables like 'sql_mode' ;

Related

MySQL 8.0.13: Default Value as uuid not working

I am trying to set the Default value as UUID() in MySQL version 8.0.13. But upon successful execution, the default value resets to NOT NULL.
MySQL version:
Here is my CREATE TABLE script
CREATE TABLE `session` (
`id` binary(16) NOT NULL DEFAULT (UUID_TO_BIN(UUID(), TRUE)),
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
the log output on table generation,
SQL script was successfully applied to the database.
The TABLE definition post execution:
CREATE TABLE `session` (
`id` binary(16) NOT NULL,
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
I am not able to figure out why this could happen when documentation clearly mentions that parenthesis enclosed functions are allowed.
This is unfortunately a bug with default expressions for primary key columns, Expression Default is made NULL during CREATE TABLE query, if field is made PK.
It is fixed in MySQL 8.0.19:
For a column defined as a PRIMARY KEY in a CREATE TABLE statement, a default value given as an expression was ignored. (Bug #29596969, Bug #94668)
As a workaround (if you cannot upgrade), you can add the primary key afterwards with an ALTER TABLE-statement:
CREATE TABLE `session` (
`id` binary(16) NOT NULL DEFAULT (UUID_TO_BIN(UUID(), TRUE)),
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE `session` ADD PRIMARY KEY(`id`);
I needed the column to not be a binary one. So, in my case, I declared it like this:
`id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT (UUID()),
For anyone who needs it to store UUIDs with default values in a char column.

mysql updated_at use milliseconds since EPOCH as default ON UPDATE

In my app I am using Sequelize. Now I want to use milliseconds since EPOCH for the updated_at column, so that it looks like:
-------------
updated_at
-------------
1571838511364
-------------
The data definition now looks like:
CREATE TABLE `projects` (
`id` char(36) NOT NULL,
`name` varchar(45) NOT NULL,
`description` varchar(450) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
I have done this to the created_at as it is easy to set the default value with Sequelize, but for updated_at, how do I set the default value for ON UPDATE?
I know
SELECT UNIX_TIMESTAMP(current_timestamp()) will give me the current millisecond, so I have:
updatedAt: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: Sequelize.literal(
'UNIX_TIMESTAMP(current_timestamp()) ON UPDATE UNIX_TIMESTAMP(current_timestamp())'
),
},
And this will give me error:
Executing:
ALTER TABLE `ab`.`projects`
CHANGE COLUMN `updated_at` `updated_at` VARCHAR(45) NOT NULL DEFAULT UNIX_TIMESTAMP(current_timestamp()) ON UPDATE UNIX_TIMESTAMP(current_timestamp()) ;
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNIX_TIMESTAMP(current_timestamp()) ON UPDATE UNIX_TIMESTAMP(current_timestamp()' at line 2
I also tried default value to be:
'UNIX_TIMESTAMP ON UPDATE UNIX_TIMESTAMP'
but I got the same sql error anyway.
How do I achieve my goal so that ON UPDATE will use the current millisecond value?
default value in mysql must a be literal , only exception is using CURRENT_TIMESTAMP() .
https://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html#data-types-defaults-explicit
unix_timestamp(CURRENT_TIMESTAMP()) is different than CURRENT_TIMESTAMP()
If you want milli-seconds or microsecond you must add a precision to datetime and current_timestamp .
You table will become this with a precision of 4 digits .
CREATE TABLE `projects` (
`id` char(36) NOT NULL,
`name` varchar(45) NOT NULL,
`description` varchar(450) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`created_at` datetime(4) NOT NULL DEFAULT CURRENT_TIMESTAMP(4),
`updated_at` datetime(4) NOT NULL DEFAULT CURRENT_TIMESTAMP(4) ON UPDATE CURRENT_TIMESTAMP(4),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;
A example with sqlfiddle ( http://sqlfiddle.com/#!9/3a85ff/1 )

Create table with default values giving error

Using MySql Workbench 6.3 Build version 6.3.6.
I am trying to create a table with Default constraint but its giving me error.
Here is the script
Create Table `Migration_Log2` (
`Id` Int NOT NULL AUTO_INCREMENT,
`FilePath` varchar(1000) NOT NULL,
`FileName` varchar(100) NOT NULL,
`IsSent` bool NOT NULL DEFAULT '0',
`CreatedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ModifiedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`SendAttemptMade` int NOT NULL DEFAULT '0',
`Message` Text DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `migration_log_Id_UNIQUE` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Error Message
Error Code: 1067. Invalid default value for 'CreatedDate' 0.000 sec
This may be due to some strict constraint on the data type check on database server.
I would suggest to change type of field CreatedDate from datetime to timestamp.
I had faced similar issue in a VPS for my website.
Your CURRENT_TIMESTAMP might have been appending the microseconds in the output.
Try to use: CURRENT_TIMESTAMP(0) as the default value.
SELECT CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(0), CURRENT_TIMESTAMP(1), CURRENT_TIMESTAMP(2);
The microseconds mattered, possibly. See the differences.

MySQL: CURRENT_TIMESTAMP for date field works locally but not on server

I have a date column whose default value is CURRENT_TIMESTAMP. It works fine locally, but when I did an export and tried to create the database on my hosting I get this:
`Invalid default value for 'created'`
...from this code:
CREATE TABLE IF NOT EXISTS `bookings` (
`id` varchar(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`cloned` int(11) DEFAULT NULL,
`event_id` varchar(11) NOT NULL,
`amount_due` smallint(6) NOT NULL,
`vat` tinyint(2) NOT NULL,
`discount` tinyint(4) DEFAULT NULL,
`date_paid` date DEFAULT NULL,
`notes` mediumtext,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The answer to this question says that, apparently, CURRENT_TIMESTAMP is an acceptable default only for datetime, not date, columns. But like I say, it works locally, just not remotely.
Could it be to do with the difference in MySQL versions?
Local MySQL: protocol v10 / server v5.6.16
Remote MySQL: protocol v10 / server v5.5.35
Am I getting away with this locally because of the higher server version?
This won't work for MySQL 5.5; before mysql 5.6.5 CURRENT_TIMESTAMP works only for TIMESTAMP.
http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
It's working for you locally, because you are using MySQL 5.6.16 there.
In the MySQL manual a Timestamp datatype is recommended for use with "DEFAULT CURRENT_TIMESTAMP".
Also consider this (taken from the MySQL manual):
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
try using
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

MySQL unknown column error when using ALTER, don't understand behaviour

I was wondering if someone could help me.
I have a odd behaviour while issueing a ALTER command. The command comes from MySQL Workbench sync and it is failing. I have a table with fields:
`id` int(11) NOT NULL AUTO_INCREMENT ,
`text` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL ,
`updated` datetime NULL DEFAULT NULL ,
`remote_addr` varchar(45) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`http_user_agent` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`user_id` int(11) NULL DEFAULT NULL ,
`category` varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`created` datetime NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
And I want to issue the ALTER command:
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
I get in response:
Unknown column 'created' in 'logs'
But
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`
works by itself, and:
ALTER TABLE `logs`
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
also works by itself.
I don't understand why when both are combined in one query it doesn't work and says that 'created' doesn't exist. I know that it definately exists.
Note that I'm not worried about the change column for 'created', it is generated by MWB when comparing and preparing to sync. But was just wondering why both actions can't be put on one query.
I am using MySQL 5.5.8
Update
I actually can do multiple clauses okay. I have been doing it on other tables just fine.
I forgot to mention this. But when I remove the AFTER part it works.
So this does not work:
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
But this does:
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
I had the same problem. I solved it by doing the CHANGE COLUMN (or MODIFY COLUMN) before ADD COLUMN.
In your example that would give the following SQL statement :
ALTER TABLE `logs`
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`,
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`;
This appears to be a bug:
http://bugs.mysql.com/bug.php?id=60650
I submitted this question as an example.
From the Documentation
You can issue multiple ADD, ALTER, DROP, and CHANGE clauses in a single ALTER TABLE statement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause per ALTER TABLE statement. For example, to drop multiple columns in a single statement, do this:
ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;
So you are only allowed to issue
ALTER TABLE t CHANGE ..., CHANGE ...
Not a combination of different modification statements.
Don't know, if the 5.5.8 has changed that behaviour, though.