[英]Laravel 7.0 MySQL SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table
System Info:系统信息:
I am building a small Laravel application and am having an issue with MySQL and relationships.我正在构建一个小型 Laravel 应用程序,并且遇到了 MySQL 和关系的问题。 When I try to run my migrations, this is the error I get:当我尝试运行我的迁移时,这是我得到的错误:
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'customers_table' (SQL: alter table `customer_contacts` add constraint `customer_contacts_customer_id_foreign` foreign key (`customer_id`) references `customers_table` (`id`))
And these are the two migration files in question.这是有问题的两个迁移文件。
customer
table and migration: customer
表和迁移:
<?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('company_name', 50);
$table->string('phone_number', 20)->nullable();
$table->string('fax_number', 20)->nullable();
$table->string('address_line_1', 75)->nullable();
$table->string('address_line_2', 75)->nullable();
$table->string('city', 75)->nullable();
$table->string('state', 30)->nullable();
$table->string('zip', 11)->nullable();
$table->string('industry', 100)->nullable();
$table->text('notes')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}
}
And the customer_contacts
table and migration:以及customer_contacts
表和迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCustomerContactsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customer_contacts', function (Blueprint $table) {
$table->id();
$table->foreignId('customer_id')->constrained('customers_table');
$table->string('name', 50);
$table->string('title', 50);
$table->string('project', 50);
$table->string('email', 50);
$table->string('mobile_phone', 20);
$table->string('work_phone', 20);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customer_contacts');
}
}
And this is the relevant section from my database.php
file:这是我的database.php
中的相关部分。php 文件:
...
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
...
I tried changing my database to SQLite to see if I get the same error and I do not, only MySQL creates this error.我尝试将我的数据库更改为 SQLite 以查看是否出现相同的错误,但我没有,只有 MySQL 会创建此错误。
Use利用
$table->foreignId('customer_id')->constrained('customers')
Instead of代替
$table->foreignId('customer_id')->constrained('customers_table');
Answer posted here: https://stackoverflow.com/a/61393131/7018549 .答案发布在这里: https://stackoverflow.com/a/61393131/7018549 。
Use利用
$table->foreignId('customer_id')->constrained('customers')
Instead of代替
$table->foreignId('customer_id')->constrained('customers_table');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.