简体   繁体   English

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

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

System Info:系统信息:

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

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.

相关问题 SQLSTATE[HY000]:一般错误:1824 无法打开引用的表“地主” - SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'landlords' SQLSTATE[HY000]:一般错误:1215 无法添加外键约束 [Laravel 7.0] - SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint [Laravel 7.0] SQLSTATE [HY000]:一般错误:Laravel发生2053错误 - SQLSTATE[HY000]: General error: 2053 error occurs at Laravel 更新表格错误? (SQLSTATE [HY000]:一般错误) - Updating table error? (SQLSTATE[HY000]: General error) PHP MySQL PDO SQLSTATE[HY000]:一般错误 - PHP MySQL PDO SQLSTATE[HY000]: General error SQLSTATE[HY000]:一般错误:无法创建表 - SQLSTATE[HY000]: General error:Can't create table SQLSTATE[HY000]:一般错误:14 无法打开数据库文件 - Laravel - SQLSTATE[HY000]: General error: 14 unable to open database file - Laravel Laravel / MySQL 错误:SQLSTATE[HY000]:一般错误:1835 格式错误的通信数据包 - Laravel / MySQL Error: SQLSTATE[HY000]: General error: 1835 Malformed communication packet laravel “SQLSTATE [HY000]:一般错误:1 没有这样的表:用户”但存在“用户”表 - laravel "SQLSTATE[HY000]: General error: 1 no such table: user" but 'users' table exists SQLSTATE [HY000]:一般错误 - SQLSTATE[HY000]: General error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM