【问题标题】:(Laravel) Getting an incorrect datetime format error when running a migration(Laravel) 运行迁移时出现不正确的日期时间格式错误
【发布时间】:2017-11-02 21:41:12
【问题描述】:

我只是想在现有表中添加一个新列。这是我的迁移:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddWarehouse extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('routes', function (Blueprint $table){
            $table->string('warehouse_address');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('routes', function (Blueprint $table){
            $table->dropColumn(['warehouse_address']);
        });
    }
}

运行 php artisan migrate 导致如下错误:

 [Illuminate\Database\QueryException]
 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1 (SQL: alter table `routes` add `warehouse_address` varchar(255) not null)



 [Doctrine\DBAL\Driver\PDOException]
 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1



 [PDOException]
 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1

该表包含已填充正确值的列“created_at”和“updated_at”。如何阻止 Schema 尝试将日期时间插入“created_at”列?我宁愿不理它。

在 MySql 中执行命令效果很好。

编辑:当我尝试在没有“created_at”和“updated_at”列的表上运行相同的迁移时,我得到了同样的错误。

【问题讨论】:

    标签: laravel-5


    【解决方案1】:

    public $timestamps = false; 放在 Route 模型中会阻止 Eloquent 尝试更新它们。

    (参见:Disable Laravel's Eloquent timestamps

    【讨论】:

    • 感谢您的评论。我注意到我对模型所做的更改不会对错误消息产生影响。即使将代码添加到应该引发语法错误的模型中也没有什么区别。这让我相信迁移没有连接到正确的模型。类名最初是复数。我在 Laravel 命名约定中出现了单数。不幸的是,这并没有解决问题。有什么建议吗?我应该注意到,该模型在我的项目的其余部分工作得非常好。
    • 好吧,如果是这样的话,第 1 行条目的时间戳似乎是 0000-00-00 00:00:00,这是无效的。可能是您的代码中的所有内容都正常工作,但默认情况下该条目被赋予了一个奇怪的时间戳。如果可能,您能否检查数据库以查看为什么该行的时间戳全为零?
    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 2013-12-07
    • 2018-03-06
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多