【问题标题】:Laravel phpunit tests will not migrate properlyLaravel phpunit 测试不会正确迁移
【发布时间】:2017-04-12 23:08:30
【问题描述】:

我有 2 张桌子,badgescounselors。我所有的测试都是绿色的。我添加了第三个表,一个数据透视表,名为 badge_counselor。这是迁移:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBadgeCounselorTable extends Migration {

  /**
   * Run the migrations.
   *
   * @return void
   */

  public function up()
  {
    Schema::create('badge_counselor', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('badge_id')->unsigned();
        $table->integer('counselor_id')->unsigned();
        $table->foreign('badge_id')->references('id')->on('badges')->onDelete('cascade');
        $table->foreign('counselor_id')->references('id')->on('counselors')->onDelete('cascade');
    });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
      Schema::dropIfExists('badge_counselor');
  }
}

当我运行 php artisan migrate / php artisan migrate:refresh / php artisan migrate:rollback 时,一切正常。但是,当我运行单元测试时,它们都失败了。并且每一个都返回错误信息:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 table "badge_counselor" already exists (SQL: create table "badge_counselor" ("id" integer not null primary key autoincrement, "badge_id" integer not null, "counselor_id" integer not null, foreign key("badge_id") references "badges"("id") on delete cascade, foreign key("counselor_id") references "counselors"("id") on delete cascade))

或者简单地说:

PDOException: SQLSTATE[HY000]: General error: 1 table "badge_counselor" already exists

从错误消息来看,假设表没有被正确删除,但是当我从终端运行migrate 命令时,它们是完美的。我尝试删除 migrations 表、所有表,甚至整个数据库并再次创建它,但似乎没有任何效果。

谢谢。

【问题讨论】:

    标签: laravel laravel-migrations


    【解决方案1】:

    [解决方案] 不完全确定发生了什么,但我删除了我用于 phpunit 的 sqlite 文件,然后再次运行它们。

    【讨论】:

      【解决方案2】:

      你在使用 MySQL 吗?如果是这样,可能会尝试删除整个数据库并重新创建它,但请确保您的数据库在 InnoDB 而不是 MyISAM 中。为此,请将 phpMyAdmin 的变量部分中的 default_storage_engine 更改为 InnoDB

      【讨论】:

      • 只记得我使用的是 sqlite,而不是 MySQL。
      猜你喜欢
      • 2020-05-02
      • 2015-05-07
      • 2017-11-29
      • 2017-03-13
      • 2015-02-05
      • 2020-06-13
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多