#6205: Invalid default value for datetime/timestamp
---------------------------+------------------------------------------------
Reporter: verbal | Owner:
Type: Test Case | Status: new
Priority: Medium | Milestone: 1.2.x.x
Component: MySQLi | Version: 1.2 Final
Severity: Normal | Resolution:
Keywords: | Php_version: PHP 5
Cake_version: 1.2.1.8004 |
---------------------------+------------------------------------------------
Comment (by verbal):
I don't know how to provide such a case, but I will write you step by step
guide how to recreate this error.
1. create databases
{{{
CREATE DATABASE `someapp`;
CREATE TABLE `someapp`.`users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT '' NOT NULL,
`password` varchar(255) DEFAULT '' NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`));
CREATE DATABASE `someapp_test`;
}}}
2. ./cake bake someapp
3. configure database connections (someapp/configs/database.php)
{{{
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysqli',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'asdf1234',
'database' => 'someapp',
'prefix' => '',
);
var $test = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'asdf1234',
'database' => 'someapp_test',
'prefix' => '',
);
}}}
4. create model user (someapp/model/user.php)
{{{
class User extends AppModel {
var $name = 'User';
}
}}}
5. create fixture (someapp/tests/fixtures/user_fixture.php)
{{{
class UserFixture extends CakeTestFixture {
var $name = 'User';
var $import = array('model' => 'User', 'records' => false);
var $records = array(
array ('id' => 1, 'name' => 'one', 'password' => 'one',
'created' => '2009-03-13 10:39:23', 'modified' => '2009-03-13 10:41:31'),
array ('id' => 2, 'name' => 'two', 'password' => 'two',
'created' => '2009-03-13 10:39:23', 'modified' => '2009-03-13 10:41:31'),
array ('id' => 3, 'name' => 'three', 'password' => 'three',
'created' => '2009-03-13 10:39:23', 'modified' => '2009-03-13 10:41:31'),
);
}
}}}
6. create test case for model (someapp/tests/cases/models/users.test.php)
{{{
App::import('Model', 'User');
class UserTestCase extends CakeTestCase {
var $fixtures = array('app.user');
function testFindById() {
$this->User =& ClassRegistry::init('User');
$result = $this->User->findById(1, array('User.id'));
$expected = array('User' => array('id' => 1));
$this->assertEqual($result, $expected);
}
}
}}}
7. go to http://domain.tld/someapp/test.php, App Test Cases, models/Users
What I got is:
{{{
Query: CREATE TABLE `users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT '' NOT NULL,
`password` varchar(255) DEFAULT '' NOT NULL,
`created` datetime DEFAULT '' NOT NULL,
`modified` datetime DEFAULT '' NOT NULL, PRIMARY KEY
(`id`));
}}}
and
{{{
Unexpected PHP error [<span style = "color:Red;text-align:left"><b>SQL
Error:</b> 1067: Invalid default value for 'created'</span>] severity
[E_USER_WARNING] in
[/usr/lib/cake1.2/cake_trunk/libs/model/datasources/dbo_source.php line
525]
/var/www/htdocs/someapp/tests/cases/models/users.test.php -> UserTestCase
-> start
}}}
Now when I create user table with timestamp columns instead of datetime
{{{
CREATE TABLE `someapp`.`users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT '' NOT NULL,
`password` varchar(255) DEFAULT '' NOT NULL,
`created` timestamp NOT NULL,
`modified` timestamp NOT NULL,
PRIMARY KEY (`id`));
}}}
I got:
{{{
Query: CREATE TABLE `test_suite_users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT '' NOT NULL,
`password` varchar(255) DEFAULT '' NOT NULL,
`created` timestamp DEFAULT 'CURRENT_TIMESTAMP' NOT NULL,
`modified` timestamp DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (`id`));
}}}
and
{{{
# Error
Unexpected PHP error [<span style = "color:Red;text-align:left"><b>SQL
Error:</b> 1067: Invalid default value for 'created'</span>] severity
[E_USER_WARNING] in
[/usr/lib/cake1.2/cake_trunk/libs/model/datasources/dbo_source.php line
525]
/var/www/htdocs/someapp/tests/cases/models/users.test.php -> UserTestCase
-> start
}}}
With timestamp, default value for created should be CURRENT_TIMESTAMP
without single quotes
(http://dev.mysql.com/doc/refman/5.0/en/timestamp.html), and for datetime
there shouldn't be any default value.[[BR]][[BR]]
CakePHP revision 8115[[BR]]
MySQL 5.0
--
Ticket URL: <https://trac.cakephp.org/ticket/6205#comment:3>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC.
Our primary goal is to provide a structured framework that enables PHP users at
all levels to rapidly develop robust web applications, without any loss to
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"tickets cakephp" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---