php - Laravel 5.7 Schema 外键 SQLSTATE[42000] 迁移

标签 php mysql sql laravel

我正在做一个学校项目,我需要使用和 erd 通过 laravel 设置我的数据库。我正在尝试让我的迁移使用外键。

我如何在 Laravel 中使用外键?

这是我的表格:

    public function up()
{
    Schema::create('customer_orders', function (Blueprint $table) {
        $table->increments('customer_order_id');
        $table->foreign('customer_id')->references('customer_id')->on('customers');
        $table->float('totaal_bedrag');
        $table->timestamps();
    });
}

    public function up()
{
    Schema::create('customers', function (Blueprint $table) {
        $table->increments('customer_id');
        $table->string('voornaam');
        $table->string('email');
        $table->string('adress');
        $table->tinyInteger('telefoon');
        $table->timestamps();
    });
}


    public function up()
{
    Schema::create('customer_order_products', function (Blueprint $table) {
        $table->increments('customer_order_product_id');
        $table->foreign('order_id')->references('order_id')->on('customer_orders');
        $table->foreign('product_id')->references('product_id')->on('products');
        $table->float('totaal_bedrag');
        $table->timestamps();
    });
}



    public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('product_id');
        $table->float('prijs_product');
        $table->string('product_naam');
        $table->timestamps();
    });
}

当我删除外键时,一切正常。 现在我收到以下错误消息:

\\\\\\\\

        C:\xampp\htdocs\newapps>php artisan migrate:fresh -v
        Dropped all tables successfully.
        Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2019_01_13_151934_create_customer_orders_table

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'customer_id' doesn't exist in table (SQL: alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`))

  at C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'customer_id' doesn't exist in table")
      C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  3   Illuminate\Database\Connection::Illuminate\Database\{closure}("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`)", [])
      C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:657

  4   Illuminate\Database\Connection::runQueryCallback("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`)", [], Object(Closure))
      C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:624

  5   Illuminate\Database\Connection::run("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` foreign key (`customer_id`) references `customers` (`customer_id`)", [], Object(Closure))
      C:\xampp\htdocs\newapps\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459

  6   Illuminate\Database\Connection::statement("alter table `customer_orders` add constraint `customer_orders_customer_id_foreign` 
        foreign key (`customer_id`) references `customers` (`customer_id`
\\\\\\\\

最佳答案

您应该在创建外键之前创建一个字段。例如,这应该有效:

Schema::create('customer_order_products', function (Blueprint $table) {
    $table->increments('customer_order_product_id');
    $table->integer('order_id')->unsigned();
    $table->integer('product_id')->unsigned();
    $table->float('totaal_bedrag');
    $table->timestamps();

    $table->foreign('product_id')->references('product_id')->on('products');
    $table->foreign('order_id')->references('order_id')->on('customer_orders');
});

还要确保您的迁移以正确的顺序运行。 productscustomer_orders 的迁移应该在上述迁移之前运行。

关于php - Laravel 5.7 Schema 外键 SQLSTATE[42000] 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54171058/

相关文章:

php - 使用 MySQL 过滤数字

mysql - SQL 调用以返回多日项目的每月天数计数

mysql - 对两列使用相同的 id 连接时出错 - MYSQL

mysql - wordpress查询的sql语句

sql - 根据另一列填充一列

mysql - 显示一个表中的所有记录以及另一表中的匹配记录

php - Ajax/php header :Location

php - 在未来的 PHP 版本中,与类同名的方法将不再是构造函数; PasswordHash 有一个已弃用的构造函数

php - 使用 Doctrine 实体和 JMSserializer 避免递归

php - 功能测试和与 GitLab 的持续集成