【问题标题】:Laravel Migrations - Column already existsLaravel 迁移 - 列已经存在
【发布时间】:2017-07-09 01:28:22
【问题描述】:

我正在运行 Laravel Framework version 5.2.45 并希望迁移我的数据库迁移。

我有两个迁移。一个是开箱即用的标准2014_10_12_100000_create_password_resets_table,另一个是我新创建的迁移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');
    }
}

做典型的php artisan migrate我得到(回滚之前):

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 $ 

在数据库内部没有创建表keywords

任何建议我做错了什么?

感谢您的回复!

【问题讨论】:

  • 尝试 composer dump 命令,然后尝试 php aristan migration:refresh

标签: php laravel laravel-5 laravel-5.2


【解决方案1】:

$table-&gt;timestamps(); 是一个始终添加created_atupdated_at 字段的函数。在您的情况下,您要添加两次。
如果您想添加另一个时间戳字段,则必须使用 timestamp() 函数。

因此,您应该将 published_at 的行替换为以下内容(删除 s):

$table->timestamp('published_at');

可以在the documentation 中找到所有可能的列类型的列表。

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 2020-03-03
    • 2012-03-11
    • 2018-04-07
    • 2017-08-28
    • 2018-03-31
    • 2019-11-17
    • 2012-04-11
    • 2013-02-04
    相关资源
    最近更新 更多