我正在Laravel做一个项目,每当我尝试迁移项目时,我都会遇到这个问题:
SQLSTATE[HY000]: General error: 1005 Can't create table `invoices`.`invoice_attachments` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `invoice_attach
ments` add constraint `invoice_attachments_invoice_id_foreign` foreign key (`invoice_id`) references `invoices` (`id`) on delete cascade)
at E:\Laravel\invoices\vendor\laravel\framework\src\Illuminate\Database\Connection.php:759
755▕ // If an exception occurs when attempting to run a query, we'll format the error
756▕ // message to include the bindings with SQL, which will make this exception a
757▕ // lot more helpful to the developer instead of just the database's errors.
758▕ catch (Exception $e) {
➜ 759▕ throw new QueryException(
760▕ $query, $this->prepareBindings($bindings), $e
761▕ );
1 E:\Laravel\invoices\vendor\laravel\framework\src\Illuminate\Database\Connection.php:544
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `invoices`.`invoice_attachments` (errno: 150 "Foreign key constraint is incorrectly formed")")
2 E:\Laravel\invoices\vendor\laravel\framework\src\Illuminate\Database\Connection.php:544
PDOStatement::execute()
我的目标是创建一个名为invoice_attachments的表,它有一个invoice_id将其连接到invoices表。下面是invoices表的代码:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if(!Schema::hasTable('invoices')) {
Schema::create('invoices', function (Blueprint $table) {
$table->id();
$table->string('invoice_number', 50);
$table->date('invoice_Date')->nullable();
$table->date('Due_date')->nullable();
$table->string('product', 50);
$table->bigInteger('section_id')->unsigned();
$table->foreign('section_id')->references('id')->on('sections')->onDelete('cascade');
$table->decimal('Amount_collection', 8, 2)->nullable();;
$table->decimal('Amount_Commission', 8, 2);
$table->decimal('Discount', 8, 2);
$table->decimal('Value_VAT', 8, 2);
$table->string('Rate_VAT', 999);
$table->decimal('Total', 8, 2);
$table->string('Status', 50);
$table->integer('Value_Status');
$table->text('note')->nullable();
$table->date('Payment_Date')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('invoices');
}
};
下面是invoice_attachments表的代码:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable('invoice_attachments')) {
Schema::create('invoice_attachments', function (Blueprint $table) {
$table->id();
$table->string('file_name', 999);
$table->string('invoice_number', 50);
$table->string('Created_by', 999);
$table->unsignedBigInteger('invoice_id')->unsigned();
//$table->foreign('invoice_id')->references('id')->on('sections')->onDelete('cascade');
$table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');
$table->timestamps();
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('invoice_attachments');
}
};
下面是项目中迁移文件的顺序:
我已尝试将发票中的ID类型更改为bigIncrement,但未成功
1条答案
按热度按时间4ngedf3f1#
在发票表中,您将使用以下内容创建ID列:
这将(我相信是从Laravel 8开始)创建一个BIGINT长度为20、无符号、不可空且为主键的列。
要在另一个表上创建列并将其引用为外键,最简单的方法是在“invoice_attachments”迁移中执行以下操作:
这将自动创建列和外键-它将在表“invoices”中查找列“id”-如果其中任何一个不正确,则可以使用以下命令指定:
尽管如此,您的代码在Laravel 9上对我来说是有效的。可能是您使用的是早期版本,其中(从内存中)$table-〉id()创建了一个12位整数,而不是20位整数,因此使用'unsignedBigInteger'失败。
我会修改您的发票迁移,以专门创建一个20位数的大栏ID,并重新运行。