为什么我不能在 Laravel 中迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43881415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:56:26  来源:igfitidea点击:

How come I can't migrate in laravel

phplaravel

提问by Lami

Migration files in laravel is used to create the tables in the database, right? But when ever I try to migrate it gives me this error:

Laravel 中的迁移文件是用来创建数据库中的表的,对吗?但是当我尝试迁移时,它会给我这个错误:

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

[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users(idint unsigned not null auto_increment primary key, namevarchar(255) not null, emailvarchar(255) not null, passwordvarchar(255) not null, remember_tokenvarchar(100) null, created_attimestamp null, updated_attim estamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

[PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

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

[Illuminate\Database\QueryException] SQLSTATE[42S01]:基表或视图已经存在:1050 表“用户”已经存在(SQL:创建表usersidint unsigned not null auto_increment 主键,namevarchar(255) not null,emailvarchar(255) ) not null, passwordvarchar(255) not null, remember_tokenvarchar(100) null, created_attimestamp null, updated_attim estamp null) 默认字符集 utf8mb4 collat​​e utf8mb4_unicode_ci)

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



and I created a new migration file and it's called test. I know users already exist but I want create the new table I created which is called test.

我创建了一个新的迁移文件,它被称为 test。我知道用户已经存在,但我想创建我创建的名为 test 的新表。

here is the migration file i am going to use to create my table but wont create:

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

    <?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');
    }
}

here is the users migration file that tells me it exist:

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

<?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');
    }
}

here is the password migration file:

这是密码迁移文件:

<?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');
    }
}

here is the dummies migration file i have that i didn't talk about because i thought it is not the problem:

这是我没有谈论的虚拟迁移文件,因为我认为这不是问题:

<?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');
    }
}

and sorry for keeping you guys waiting it took me long to find the edit button and fix the spacing of my code. It is my first time using stack over flow.

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

回答by fubar

The migrationstable in your database is out of sync with the rest of your database. Laravel is trying to re-run a migration to create the userstable and failing.

migrations数据库中的表与数据库的其余部分不同步。Laravel 正在尝试重新运行迁移以创建users表并失败。

If this is an entirely new project, you could delete all tables from your database and re-run all migrations. Alternatively, fix your migration table so that it accurately reflects the state of your database.

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

回答by Vandolph Reyes

In your laravel/app/providers/AppServiceProvider.php add the following code inside boot

在你的 laravel/app/providers/AppServiceProvider.php 中,在 boot 中添加以下代码

Schema::defaultStringLength(191);

Then try php artisan migrate:refresh

然后试试 php artisan migrate:refresh

or best

或最好

drop all tables and run php artisan migrateagain

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

回答by Nitesh Kumar Niranjan

Add following code in the start of your up function() of all tables

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

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

回答by Tanvir Sajal

Try this...

尝试这个...

*// AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

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