简体   繁体   English

由于某种原因,我无法迁移

[英]I can't migrate for some reason 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 to create the new table I created which is called test. 我知道用户已经存在,但是我想创建自己创建的新表,称为test。 I dropped all my tables and re-migrate it but it only created my users and migration table again. 我删除了所有表并重新迁移,但它仅再次创建了我的用户和迁移表。 Not the new table I wanted to create which is 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 also wanted to create but wont create for some reason even after i drop the all the tables: 这是我也想创建的虚拟人迁移文件,但是即使删除了所有表,由于某种原因也不会创建:

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

Are you using this command ? 您正在使用此命令吗? php artisan migrate:refresh

If you do then try this : 如果您这样做,请尝试以下操作:

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

And do the same thing in your others migrations files , that way, the table will be drop before every migration and you not have your problem anymore :) (it is quite the same thing as a php artisan migrate:refresh 并在您的其他迁移文件中执行相同的操作,这样,该表将在每次迁移之前删除,并且您不再遇到问题了:)(与php artisan migrate:refresh migration完全相同php artisan migrate:refresh

I think you should try this: 我认为您应该尝试这样做:

first remove tests, users, password_resets, dummies from migrations table in your database after then run php artisan migrate command 首先从数据库中的迁移表中删除测试,用户,password_resets,虚拟变量,然后运行php artisan migrate命令

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

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