Hi i am trying to make an invoice app in Laravel and i got stuck, I am trying to run php artisan migrate but I get an error
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `invoices` add constraint `invoices_customer_id_foreign` foreign key (`customer_id`) references `customers` (`id`))
at C:\laragon\www\zorland-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
1 C:\laragon\www\zorland-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
2 C:\laragon\www\zorland-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOStatement::execute()
I tried to run php artisan migrate:refresh fresh, rollback but with no sucess. I use HeidiSQL and when i run php artisan make:model InvoicesItem -mi could not create the table so i tried to create it in SQL database. Then i dropped the SQL database and tried again and i got this error.
InvoiceItem.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class InvoicesItem extends Model
{
protected $fillable = ['invoice_id', 'name', 'quantity', 'price'];
}
InvoicesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Invoice;
use App\InvoicesItem;
class InvoicesController extends Controller
{
//
public function create()
{
return view('invoices.create');
}
public function store(Request $request)
{
$customer = Customer::create($request->customer);
$invoice = Invoice::create($request->invoice + ['customer_id' => $customer->id]);
for ($i=0; $i < count($request->product); $i++) {
if (isset($request->qty[$i]) && isset($request->price[$i])) {
InvoicesItem::create([
'invoice_id' => $invoice->id,
'name' => $request->product[$i],
'quantity' => $request->qty[$i],
'price' => $request->price[$i]
]);
}
}
return 'To be continued';
}
}
Migrations
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateInvoicesItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('invoices_items', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('customer_id');
$table->foreign('customer_id')->references('id')->on('customers');
$table->string('field_key');
$table->string('field_value');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('invoices_items');
}
}
customers table migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('address');
$table->string('postcode')->nullable();
$table->string('city');
$table->string('state')->nullable();
$table->string('country');
$table->string('phone')->nullable();
$table->string('email')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}
}
In CreateInvoicesItemsTable
migration file, you are using the wrong data type for customer_id
. The data type for customer_id
column will be unsignedBigInteger
Replace
$table->unsignedInteger('customer_id');
With
$table->unsignedBigInteger('customer_id');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.