繁体   English   中英

我为什么不能在Laravel迁移

[英]How come I can't migrate in laravel

laravel中的迁移文件用于在数据库中创建表,对吗? 但是,每当我尝试迁移时,都会出现此错误:

C:\\ xampp \\ htdocs \\ app> php artisan迁移

[Illuminate \\ Database \\ QueryException] SQLSTATE [42S01]:基本表或视图已存在:1050表'users'已存在(SQL:创建表usersid int unsigned不为null auto_increment主键, name varchar(255)不为null, email VARCHAR(255)NOT NULL, password为varchar(255)NOT NULL, remember_token为varchar(100)空, created_at时间戳空, updated_at添estamp空)默认字符集utf8mb4整理utf8mb4_unicode_ci)

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


我创建了一个新的迁移文件,称为测试。 我知道用户已经存在,但是我想创建我创建的新表,称为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');
    }
}

很抱歉让你们久等了,我花了很长时间才找到编辑按钮并修复代码间距。 这是我第一次使用堆栈溢出。

数据库中的migrations表与数据库的其余部分不同步。 Laravel试图重新运行迁移以创建users表,但失败了。

如果这是一个全新项目,则可以从数据库中删除所有表,然后重新运行所有迁移。 或者,修复您的迁移表,以使其准确反映数据库的状态。

在您的laravel / app / providers / AppServiceProvider.php中,在启动内添加以下代码

Schema::defaultStringLength(191);

然后尝试php artisan migrate:refresh

或最好

删除所有表并再次运行php artisan migrate

在所有表的up function()开头添加以下代码

public function up()
{
        // Add this line
        Schema::dropIfExists('users');
        // from you down method
        Schema::create('users', function (Blueprint $table) {
            ...........
        });
}

尝试这个...

*// AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

function boot()
{
    Schema::defaultStringLength(191);
}
*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM