0

I'm simply trying to add a new column to an existing table. This is my migration:

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']);
        });
    }
}

Running php artisan migrate caused the following errors:

 [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

The table contains columns "created_at" and "updated_at" which are already filled with correct values. How do I stop Schema from trying to insert a datetime into the 'created_at' column? I would rather leave it alone.

Executing the commands in MySql works perfectly fine.

Edit: I get the same error when I try to run the same migration on a table without the "created_at" and "updated_at" columns.

p f
  • 1
  • 2

1 Answers1

0

Placing public $timestamps = false; in your Route model will stop Eloquent from trying to update them.

(See: Disable Laravel's Eloquent timestamps)

garrettmills
  • 660
  • 6
  • 13
  • Thanks for your comment. I've noticed that changes I make to the model don't make a difference to the error message. Even adding code to the model that should throw a syntax error makes no difference. This leads me to believe the migration isn't hooked up to the proper model. The class name was originally plural. I made singular in occurrence with Laravel naming convention. Unfortunately, this did not solve the problem. Any advice? I should note that the model works perfectly fine in the rest of my project. – p f Jun 02 '17 at 16:06
  • Well, if that's the case, it appears that the row 1 entry has a timestamp of `0000-00-00 00:00:00`, which is not valid. It may be the case that everything in your code is working properly, but that entry was given a weird timestamp by default. If possible, can you check the db to see why that row has all zeros for a timestamp? – garrettmills Jun 02 '17 at 23:04