William Narcey William Narcey - 2 months ago 20
MySQL Question

PHP7 MySQL Invalid default value for 'expiry_date' PDO Database Install

Recently installed php7.0-fpm + Nginx + MySQL on a Ubuntu 16 x64 box.

When getting a php installer to PDO to a database in order to add files it does the following:

There seems to be an error. Please try again.
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'expiry_date' in /var/www/mcdospace/install/index.php:31 Stack trace: #0 /var/www/mcdospace/install/index.php(31):
PDOStatement->execute(Array) #1 /var/www/mcdospace/install/index.php(152): try_query(Array) #2 {main}
PDOException: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in /var/www/mcdospace/install/index.php:31 Stack trace: #0 /var/www/mcdospace/install/index.php(31): PDOStatement->execute(Array) #1 /var/www/mcdospace/install/index.php(152): try_query(Array) #2 {main}
PDOException: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in /var/www/mcdospace/install/index.php:31 Stack trace: #0 /var/www/mcdospace/install/index.php(31): PDOStatement->execute(Array) #1 /var/www/mcdospace/install/index.php(152): try_query(Array) #2 {main}
PDOException: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in /var/www/mcdospace/install/index.php:31 Stack trace: #0 /var/www/mcdospace/install/index.php(31): PDOStatement->execute(Array) #1 /var/www/mcdospace/install/index.php(152): try_query(Array) #2 {main}
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' in /var/www/mcdospace/install/index.php:31 Stack trace: #0 /var/www/mcdospace/install/index.php(31): PDOStatement->execute(Array) #1 /var/www/mcdospace/install/index.php(152): try_query(Array) #2 {main}


Have I missed something in setup? It was all working fine on a Ubuntu 14 php5 system.

Heres some of the table insertion to help assess:

'0' => array(
'table' => TABLE_FILES,
'query' => 'CREATE TABLE IF NOT EXISTS `'.TABLE_FILES.'` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` text NOT NULL,
`filename` text NOT NULL,
`description` text NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`uploader` varchar('.MAX_USER_CHARS.') NOT NULL,
`expires` INT(1) NOT NULL default \'0\',
`expiry_date` TIMESTAMP NOT NULL,
`public_allow` INT(1) NOT NULL default \'0\',
`public_token` varchar(32) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
',
'params' => array(),
),

Answer

For the expiry_date column, the not null constraint without any default causes the default to be set as 0, which is not accepted as a default value in MySQL for timestamps.

Use a default timestamp for the expiry_date field.

You can check the discussion on the following thread regarding the issue.

https://github.com/laravel/framework/issues/3602