2017-06-01 119 views
0

我只是試圖向現有表添加新列。這是我的移民:(Laravel)運行遷移時獲取不正確的日期時間格式錯誤

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工匠遷移引起以下錯誤:

[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」列的表上運行相同的遷移時,我得到相同的錯誤。

回答

0

在您的路線模型中放置public $timestamps = false;將停止Eloquent嘗試更新它們。

(請參閱:Disable Laravel's Eloquent timestamps

+0

感謝您的評論。我注意到,我對模型所做的更改對錯誤消息沒有影響。即使嚮應該拋出語法錯誤的模型添加代碼也沒有區別。這導致我相信移民並沒有與正確的模式掛鉤。類名最初是複數。我在Laravel命名約定中做出了單數。不幸的是,這並沒有解決問題。有什麼建議?我應該注意到這個模型在我的其餘項目中工作得很好。 –

+0

好吧,如果是這樣的話,看起來第1行條目的時間戳是'0000-00-00 00:00:00',這是無效的。可能會出現代碼中的所有內容都正常工作,但默認情況下該條目被賦予了奇怪的時間戳。如果可能,你可以檢查數據庫,看看爲什麼該行的時間戳全部爲零? – glmdev