Laravel,Column已存在:1060重复的列名

时间:2017-05-16 21:44:22

标签: php laravel

我正在运行laravel版本5.4.22

我在终端中使用php artisan migrate:rollback然后在错误消息下面显示

  

H:\ wamp_server \ www \ cms> php artisan migrate:rollback

     

[Illuminate \ Database \ QueryException] SQLSTATE [42S21]:列   已存在:1060重复列名'is_admin'(SQL:更改   table posts add is_admin int not null)

     

[PDOException] SQLSTATE [42S21]:列已存在:1060   重复列名称'is_admin'

下面的迁移代码

    <?php

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

class AddIsAdminColumnToPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
            $table->integer('is_admin')->unsigned();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            //

            $table->integer('is_admin');
        });
    }
}

3 个答案:

答案 0 :(得分:3)

你的代码错了,你也在down函数中添加了列,请参阅下面的代码来修复:

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    try {
        Properties p = new Properties();

        p.setProperty(
                DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
                "com.example.some.app");

        Properties extras = new Properties();
        extras.setProperty("Key1", "TestString1");
        extras.setProperty("Key2", "TestString2");
        StringWriter sw = new StringWriter();
        try{
            extras.store(sw, "admin extras bundle");
            p.put(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE,
                    sw.toString());
            Log.d(TAG, "Admin extras bundle=" + p.get(
                    DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE));
        } catch (IOException e) {
            Log.e(TAG, "Unable to build admin extras bundle");
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        OutputStream out = new ObjectOutputStream(bos);
        p.store(out, "");
        final byte[] bytes = bos.toByteArray();

        NdefMessage msg = new NdefMessage(NdefRecord.createMime(
                DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC, bytes));
        return msg;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Laravel doc,显示:https://laravel.com/docs/5.4/migrations#dropping-columns

答案 1 :(得分:0)

我遇到了同样的问题,列已存在:重复的列名'created_at'

我认为是由于

$table->timepstamps('dob');

我以前的代码

public function up()
    {
        Schema::create('authors', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps('dob');
            $table->timestamps();
        });
    }

当我删除时间戳时,例如

 $table->timestamp('dob');

对我来说很好。

答案 2 :(得分:0)

我有同样的问题。所以,我向作曲家寻求帮助:))

composer require doctrine/dbal

然后:

composer update 

//当您尝试将现有列更新到数据库中时可能会遇到此问题。

您可以阅读有关此 here 的更多信息。