Laravel 7.0 MySQL SQLSTATE[HY000]: 一般错误: 1824 打不开引用的表。

系统信息。

  • 操作系统: Ubuntu 19.10
  • Laravel版本: 7.0
  • PHP版本:7.3.11-0ubuntu0.19.10.4。
  • MySQL版本:mysql Ver 8.0.19-0ubuntu0.19.10.3 for Linux on x86_64 ((Ubuntu))

我正在构建一个小型的Laravel应用程序, 但在MySQL和关系方面遇到了一个问题. 当我尝试运行我的迁移时, 我得到的错误是这样的:

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`))

这是两个有问题的迁移文件:

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');
    }
}

还有… 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');
    }
}

这是我的相关章节 database.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'),
            ]) : [],
        ],

...

我试着把我的数据库改成SQLite,看看是否会出现同样的错误,但没有,只有MySQL会出现这个错误。

解决方案:

使用

$table->foreignId('customer_id')->constrained('customers')

而不是

$table->foreignId('customer_id')->constrained('customers_table');

给TA打赏
共{{data.count}}人
人已打赏
未分类

用日期列替换Pandas Dataframe页眉,但按升序排列。

2022-9-8 8:48:36

未分类

docker操作错误(django项目)

2022-9-8 8:48:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索