简体   繁体   English

我为什么不能在Laravel迁移

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

Migration files in laravel is used to create the tables in the database, right? laravel中的迁移文件用于在数据库中创建表,对吗? But when ever I try to migrate it gives me this error: 但是,每当我尝试迁移时,都会出现此错误:

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

[Illuminate\\Database\\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (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) default character set utf8mb4 collate utf8mb4_unicode_ci) [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]: Base table or view already exists: 1050 Table 'users' already exists [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。

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. 这是我第一次使用堆栈溢出。

The migrations table in your database is out of sync with the rest of your database. 数据库中的migrations表与数据库的其余部分不同步。 Laravel is trying to re-run a migration to create the users table and failing. 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. 或者,修复您的迁移表,以使其准确反映数据库的状态。

In your laravel/app/providers/AppServiceProvider.php add the following code inside boot 在您的laravel / app / providers / AppServiceProvider.php中,在启动内添加以下代码

Schema::defaultStringLength(191);

Then try php artisan migrate:refresh 然后尝试php artisan migrate:refresh

or best 或最好

drop all tables and run php artisan migrate again 删除所有表并再次运行php artisan migrate

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) {
            ...........
        });
}

Try this... 尝试这个...

*// 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