CakePHP 4 datetime migrations error 'Incorrect datetime value'

jvlzgdj9  于 2023-04-19  发布在  PHP
关注(0)|答案(2)|浏览(116)

我正在用PHP 7.4(cakephp 4.4)开发一个应用程序。
我试图使用cakephp bake migrations功能来使用迁移来正确更新我的表,但我遇到了datetime列的问题。
下面是我的代码:

bin/cake bake migration AddInvitationTokenToUsers invitation_token:string:unique invitation_token_expires_at:datetime

输出:

Creating file /Applications/MAMP/htdocs/app/config/Migrations/20230416152709_AddInvitationTokenToUsers.php
Wrote `/Applications/MAMP/htdocs/app/config/Migrations/20230416152709_AddInvitationTokenToUsers.php`

然后启动迁移:

bin/cake migrations migrate

我有这个错误:

PDOException: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'invitation_token_expires_at' at row 1 in /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:192
Stack trace:
#0 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(192): PDO->exec('ALTER TABLE `us...')
#1 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Util/AlterInstructions.php(108): Phinx\Db\Adapter\PdoAdapter->execute('ALTER TABLE `us...')
#2 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(626): Phinx\Db\Util\AlterInstructions->execute('ALTER TABLE `us...', Array)
#3 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(1001): Phinx\Db\Adapter\PdoAdapter->executeAlterSteps('users', Object(Phinx\Db\Util\AlterInstructions))
#4 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(477): Phinx\Db\Adapter\PdoAdapter->executeActions(Object(Phinx\Db\Table\Table), Array)
#5 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/TimedOutputAdapter.php(420): Phinx\Db\Adapter\AdapterWrapper->executeActions(Object(Phinx\Db\Table\Table), Array)
#6 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(477): Phinx\Db\Adapter\TimedOutputAdapter->executeActions(Object(Phinx\Db\Table\Table), Array)
#7 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Plan/Plan.php(151): Phinx\Db\Adapter\AdapterWrapper->executeActions(Object(Phinx\Db\Table\Table), Array)
#8 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Table.php(719): Phinx\Db\Plan\Plan->execute(Object(Migrations\CakeAdapter))
#9 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Db/Table.php(627): Phinx\Db\Table->executeActions(true)
#10 /Applications/MAMP/htdocs/app/vendor/cakephp/migrations/src/Table.php(152): Phinx\Db\Table->update()
#11 /Applications/MAMP/htdocs/app/config/Migrations/20230416144739_AddInvitationTokenToUsers.php(27): Migrations\Table->update()
#12 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php(108): AddInvitationTokenToUsers->change()
#13 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php(388): Phinx\Migration\Manager\Environment->executeMigration(Object(AddInvitationTokenToUsers), 'up', false)
#14 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php(359): Phinx\Migration\Manager->executeMigration('default', Object(AddInvitationTokenToUsers), 'up', false)
#15 /Applications/MAMP/htdocs/app/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php(122): Phinx\Migration\Manager->migrate('default', 20230416144739, false)
#16 /Applications/MAMP/htdocs/app/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(37): Phinx\Console\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /Applications/MAMP/htdocs/app/vendor/cakephp/migrations/src/Command/Phinx/Migrate.php(85): Migrations\Command\Phinx\Migrate->parentExecute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /Applications/MAMP/htdocs/app/vendor/symfony/console/Command/Command.php(298): Migrations\Command\Phinx\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /Applications/MAMP/htdocs/app/vendor/symfony/console/Application.php(1040): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /Applications/MAMP/htdocs/app/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand(Object(Migrations\Command\Phinx\Migrate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /Applications/MAMP/htdocs/app/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 /Applications/MAMP/htdocs/app/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(126): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 /Applications/MAMP/htdocs/app/vendor/cakephp/cakephp/src/Console/BaseCommand.php(189): Migrations\Command\MigrationsCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#24 /Applications/MAMP/htdocs/app/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(198): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#25 /Applications/MAMP/htdocs/app/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Migrations\Command\MigrationsCommand->run(Array, Object(Cake\Console\ConsoleIo))
#26 /Applications/MAMP/htdocs/app/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Migrations\Command\MigrationsMigrateCommand), Array, Object(Cake\Console\ConsoleIo))
#27 /Applications/MAMP/htdocs/app/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#28 {main}

我的迁移文件是这样的:

<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class AddInvitationTokenToUsers extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change(): void
    {
        $table = $this->table('users');
        $table->addColumn('invitation_token', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('invitation_token_expires_at', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addIndex([
            'invitation_token',
        
            ], [
            'name' => 'UNIQUE_INVITATION_TOKEN',
            'unique' => true,
        ]);
        $table->update();
    }
}

你知道这个问题的原因吗?
谢谢

bt1cpqcv

bt1cpqcv1#

您的sql_mode配置可能不允许零日期,请检查**NO_ZERO_DATE如何工作**。
由于表中已有行,因此应该根据DBMS配置为新列提供有效的datetime默认值(例如'CURRENT_TIMESTAMP'),或者使该列可为空。
由于该列是令牌的过期时间,因此您很可能不希望将datetime值设置为默认值,因此您将选择后者,即将null选项的值更改为true以使该列可为空,从而使用null作为默认值。

ffdz8vbo

ffdz8vbo2#

https://dev.mysql.com/doc/refman/8.0/en/datetime.html
DATETIME类型用于同时包含日期和时间部分的值。MySQL以'YYYY-MM-DD hh:mm:ss'格式检索和显示DATETIME值。支持的范围是'1000-01-01 00:00:00'到'9999-12-31 23:59:59'。
您的“0000-00-00 00:00:00”小于支持的值。

相关问题