我在Laravel

时间:2017-05-10 12:53:14

标签: php laravel

laravel中的迁移文件用于在数据库中创建表,对吧?但是,当我尝试迁移时,它给了我这个错误:

  

C:\ xampp \ htdocs \ app> php artisan migrate

     

[照亮\数据库\ QueryException]     SQLSTATE [42S01]:基表或视图已存在:1050表'users'已存在(SQL:create table users id int unsigned not null auto_increment primary key,name varchar( 255)not null,email varchar(255)not null,     password varchar(255)not null,remember_token varchar(100)null,created_at timestamp null,updated_at tim     estamp null)默认字符集utf8mb4 collat​​e utf8mb4_unicode_ci)

     

[PDOException]     SQLSTATE [42S01]:基表或视图已存在:1050表'用户'已存在

我创建了一个新的迁移文件,它叫做test。我知道用户已经存在,但我想创建我创建的新表,称为test。我删除了所有表并重新迁移它,但它只是再次创建了我的用户和迁移表。不是我想创建的新表,也就是测试。

这是我将用于创建我的表但不会创建的迁移文件:

    <?php

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

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

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

这是用户迁移文件,告诉我它存在:

<?php

use Illuminate\Support\Facades\Schema;
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->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

这是密码迁移文件:

<?php

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

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

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

这里是我想创建的假人迁移文件,但即使在我删除所有表后也不会因某些原因创建:

<?php

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

class CreateDummiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dummies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('body');
            $table->timestamp('date'); //if you dont put name for the timestamp it will create: create_at and update_at fields.
        });
    }

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

2 个答案:

答案 0 :(得分:0)

您使用此命令吗? php artisan migrate:refresh

如果你这样做,那就试试这个:

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('users');
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

在您的其他迁移文件中执行相同的操作,这样,每次迁移之前表都会丢失,您不再有问题了:) (它与php artisan migrate:refresh

完全相同

答案 1 :(得分:0)

我想你应该试试这个:

首先从数据库中的迁移表中删除测试,用户,password_resets和dummies,然后运行php artisan migrate命令