Search code examples
phpmysqllaravel-5create-tablelaravel-migrations

Databse migration throws error while seeding two timestamp columns laravel 5


I extended the default migration to include some additional table fields for my user table.

I wanted to have created_at and updated_at fields with timestamps as values.

Here is my code

<?php

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

 class CreateUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->bigIncrements('id');
                    $table->bigInteger('group_id');
        $table->string('fname',255);
        $table->string('lname',255);
        $table->string('email',255)->unique();
        $table->string('password', 60);
        $table->boolean('active');
        $table->string('gravtar',255)->nullable();
        $table->rememberToken();
        $table->timestamps('created_at');
        $table->timestamps('updated_at');

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users');
}

}

The obstacle is using two timestamps columns does not migrate the tables and throws this exception

[PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created_at'

See, i have only one column with name created_at , so this exception make no sense. However when i drop one of the timestamps field, table get migrated.

I have no clue what causing this?


Solution

  • timestamps() method adds both created_at and updated_at columns. Also this method doesn't accept any arguments.

    http://laravel.com/api/5.0/Illuminate/Database/Schema/Blueprint.html#method_timestamps http://laravel.com/docs/5.0/schema