php - 错误问题:1215 无法添加外键约束
问题描述
如何解决这个错误?SQLSTATE [HY000]:一般错误:1215 无法添加外键约束(SQL:删除级联时更改表invoices
添加约束invoices_form_id_foreign
外键(form_id
)引用forms
( ))id
我得到它之后: php artisan migrate:fresh
对于发票中的外键,会出现此错误。3 个外键生成错误,但 user_id 的一个很好并且工作正常。我尝试了所有解决方案,但没有奏效。请帮我。
2014_10_12_000000_create_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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->timestamp('email_verified_at')->nullable();
$table->string('surname')->nullable();
$table->string('showname')->nullable();
$table->string('business')->nullable();
$table->string('NIP')->nullable();
$table->string('PESEL')->nullable();
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('postalcode')->nullable();
$table->string('phone')->nullable();
$table->string('comments')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
2020_07_23_104440_invoices.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Invoices extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('invoices', function (Blueprint $table) {
$table->increments('id');
$table->string('invoicenumber')->nullable();
$table->date('invoicedate')->nullable();
$table->date('selldate')->nullable();
$table->integer('user_id')->unsigned()->nullable();
$table->integer('form_id')->unsigned()->nullable();
$table->integer('currency_id')->unsigned()->nullable();
$table->integer('proform_id')->unsigned()->nullable();
$table->string('paymentmethod')->nullable();
$table->date('paymentdate')->nullable();
$table->string('status')->nullable();
$table->string('comments')->nullable();
$table->string('city')->nullable();
$table->string('paid')->nullable();
$table->string('autonumber')->nullable();
$table->string('automonth')->nullable();
$table->string('autoyear')->nullable();
$table->timestamps();
});
Schema::table('invoices', function (Blueprint $table){
$table->foreign('user_id')
->references('id')
->on('users');
$table->foreign('form_id')
->references('id')
->on('forms');
$table->foreign('currency_id')
->references('id')
->on('currencys');
$table->foreign('proform_id')
->references('id')
->on('proforms');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('invoices');
}
}
2020_07_27_090356_proforms.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Proforms extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('proforms', function (Blueprint $table) {
$table->increments('id');
$table->string('proformnumber')->nullable();
$table->date('proformdate')->nullable();
$table->date('selldate')->nullable();
$table->integer('user_id')->unsigned()->nullable();
$table->string('paymentmethod')->nullable();
$table->date('paymentdate')->nullable();
$table->string('status')->nullable();
$table->string('comments')->nullable();
$table->timestamps();
});
Schema::table('proforms', function (Blueprint $table){
$table->foreign('user_id')
->references('id')
->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('proforms');
}
}
2020_07_28_091856_forms.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Forms extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->increments('id');
$table->string('form')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('forms');
}
}
2020_07_28_091919_currencys.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Currencys extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('currencys', function (Blueprint $table) {
$table->increments('id');
$table->string('currency')->nullable();
$table->string('course')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('currencys');
}
}
解决方案
以下是您的迁移可能发生的情况。
1.检查主键和外键类型。如果用户主键ID是type $table->increments('id'); // bigInteger
,那么外键也应该是bigInteger $table->bigInteger('user_id')
;
2.请检查您的迁移顺序。例如表invoices
具有来自 的外键forms table
。在迁移过程中,forms
表应该首先迁移。为了让它发生,只需重命名迁移文件(文件名的数字部分),以便表格。
推荐阅读
- maven - Maven中updatePolicy的默认值是什么
- node.js - 在沃尔玛开放 API 的 JSON 响应中跳过 HTML 标签
- python - 使用熊猫数据框过滤带有句子的行
- php - 用逗号将行添加到 csv
- php - 通过 AJAX 发送 HTML 单选按钮选择
- angular - 允许 Angular 指令一次在单个元素上执行
- amazon-web-services - Kubernetes nginx ingress 周期性地给出 404
- python - 使用机器学习进行逻辑类曲线拟合
- kubernetes - Kubernetes 服务运行良好但无法从外部访问
- angularjs - 我如何从外部函数调用控制器中的内部函数?在使用 angularjs 获取 Typeerror 进行 typeahead 工作时: