migrate 还原数据库报错
我还原数据库结构的时候报错
php artisan migrate:refresh --seed
错误提示:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `dn_user_test_favs` ad
d constraint `dn_user_test_favs_testpsy_id_foreign` foreign key (`testpsy_id`) references `dn_testpsies` (`id`) on delete cascade)
1、我百度了一下,包括在LK也找到一篇文章( 博客:创建外键时报 Cannot add foreign key constraint 解决方法 ),说的也是这个问题,我对照了修改,把字段类型统一了,还是报错,请教是什么问题;
2、这个问题比较奇怪,我在家refresh是好的,但是在办公室就不行,网上说可能是引擎的问题,我检查了一下默认都是inno;
以下是报错提到的两个migrations:
2020_01_25_183559_create_user_test_favs_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserTestFavsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_test_favs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('testpsy_id');
$table->foreign('testpsy_id')->references('id')->on('testpsies')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_test_favs');
}
}
2020_01_22_134547_create_testpsies_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestPsiesTable extends Migration
{
public function up()
{
Schema::create('test_psies', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('title')->index();
$table->string('cover')->nullable();
$table->text('body')->nullable();
$table->text('subject');
$table->text('result');
$table->string('remind_time')->nullable();
$table->decimal('price', 10, 2)->unsigned()->nullable();
$table->decimal('sale', 10, 2)->unsigned()->nullable();
$table->bigInteger('user_id')->unsigned()->index();
$table->integer('test_category_id')->unsigned()->index();
$table->integer('last_reply_user_id')->unsigned()->default(0);
$table->integer('order')->unsigned()->default(0);
$table->text('description')->nullable();
$table->integer('reply_count')->unsigned()->default(0);
$table->integer('view_count')->unsigned()->default(0);
$table->integer('fav_count')->unsigned()->default(0);
$table->integer('do_count')->unsigned()->default(0);
$table->integer('sell_count')->unsigned()->default(0);
$table->string('slug')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::drop('test_psies');
}
}
on的表名不对,看不粗来么
改成