Carol.Kar
Carol.Kar

Reputation: 5345

Laravel Migrations - Column already exists

I am running Laravel Framework version 5.2.45 and want to migrate my database migrations.

I have two migrations. One is the standard 2014_10_12_100000_create_password_resets_table that comes out of the box and the other one is my newly created migration 2017_02_19_172350_create_keywords_table:

<?php

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

class CreateKeywordsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('keywords', function (Blueprint $table) {
            $table->increments('id');
            $table->string('keyword');
            $table->timestamps();
            $table->timestamps('published_at');

        });
    }

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

Doing the typical php artisan migrate I get(before rolling back):

root:~/workspace $ php artisan migrate:rollback
Rolled back: 2014_10_12_100000_create_password_resets_table
root:~/workspace $ php artisan migrate


  [Illuminate\Database\QueryException]                                                                                                                 
  SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created_at' (SQL: create table `keywords` (`id` int unsigned not null auto_incr  
  ement primary key, `keyword` varchar(255) not null, `created_at` timestamp null, `updated_at` timestamp null, `created_at` timestamp null, `updated  
  _at` timestamp null) default character set utf8 collate utf8_unicode_ci)                                                                             



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


root:~/workspace $ 

Inside the database no table keywords is created.

enter image description here

Any suggestions what I am doing wrong?

I appreciate your replies!

Upvotes: 2

Views: 5268

Answers (1)

Jerodev
Jerodev

Reputation: 33186

$table->timestamps(); is a function that always will add the created_at and updated_at fields. In your case, you are adding these twice.
If you want to add another timestamp field, you have to use the timestamp() function.

So you should replace the line for published_at with the following (remove the s):

$table->timestamp('published_at');

A list of all possible column types can be found in the documentation.

Upvotes: 5

Related Questions