繁体   English   中英

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

[英]Laravel 7.0 MySQL SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table

系统信息:

  • 操作系统: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中的相关部分。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');

答案发布在这里: https://stackoverflow.com/a/61393131/7018549

利用

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

代替

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM