【问题标题】:How come I can't migrate in laravel为什么我不能在laravel中迁移
【发布时间】:2017-10-08 10:47:04
【问题描述】:

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

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

[照亮\数据库\查询异常] SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在(SQL:创建表 users ( id int unsigned not null auto_increment 主键,name varchar(255) not null,@ 987654324@ varchar(255) 不为空, 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)

[PDO异常] 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');
    }
}

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

【问题讨论】:

    标签: php laravel


    【解决方案1】:

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

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

    【讨论】:

    • 我删除了数据库中的所有表,但它仍然不会创建我的新表。它只创建了我的用户表和迁移表。它没有创建我的测试表。
    • @Lami 这次你收到了什么错误?您能否发布所有迁移的代码(包括文件名)。
    • @Vandolph Reyes 我编辑了我的帖子并添加了测试迁移文件。
    • @Lami - 你必须有多个迁移文件,因为 Laravel 抱怨有一个 users 表。请包括所有迁移及其文件名。文件名很重要,因为它们表示执行迁移的顺序。
    【解决方案2】:

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

    Schema::defaultStringLength(191);
    

    那就试试php artisan migrate:refresh

    或最好的

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        试试这个...

        *// AppServiceProvider.php
        
        use Illuminate\Support\Facades\Schema;
        
        function boot()
        {
            Schema::defaultStringLength(191);
        }
        *
        

        【讨论】:

          猜你喜欢
          • 2021-06-09
          • 1970-01-01
          • 2020-11-10
          • 2020-02-20
          • 2020-11-01
          • 2021-01-12
          • 1970-01-01
          • 2019-11-23
          • 2020-07-08
          相关资源
          最近更新 更多