我有 2 张桌子,badges
并且counselors
. 我所有的测试都是绿色的。我添加了第三个表,一个数据透视表,名为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
表、所有表,甚至整个数据库并再次创建它,但似乎没有任何效果。
谢谢。