T
T
Terroris3372020-09-22 10:50:51
Laravel
Terroris337, 2020-09-22 10:50:51

How to solve the link creation error?

I can not link tables through migration gives an error:

SQLSTATE[HY000]: General error: 1005 Can't create table `mrdoner`.`#sql-c90_65` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `order_product` add constraint `order_product_order_id_foreign` foreign
key (`order_id`) references `id` (`orders`))

  at D:\Programs\xampp\htdocs\mrdoner\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675|

  1   D:\Programs\xampp\htdocs\mrdoner\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
      PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `mrdoner`.`#sql-c90_65` (errno: 150 "Foreign key constraint is incorrectly formed")")

  2   D:\Programs\xampp\htdocs\mrdoner\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
      PDOStatement::execute()


Products:
Schema::create('products', function (Blueprint $table) {
            $table->id()->unsigned();
            $table->string('name')->unique();
            $table->string('desc');
            $table->integer('price')->unsigned();
            $table->boolean('top');
            $table->foreignId('category_id');

            $table->foreign('category_id')->references('id')->on('categories');
            $table->timestamps();
        });

Orders:
Schema::create('orders', function (Blueprint $table) {
            $table->id()->unsigned();
            $table->string('name');
            $table->string('phone_number');
            $table->string('address');
            $table->string('status');
            $table->string('payment_method');
            $table->timestamps();
        });

order_product:
Schema::create('order_product', function (Blueprint $table) {
            $table->foreignId('order_id');
            $table->foreignId('product_id');

            $table->foreign('order_id')->on('id')->references('orders');
            $table->foreign('product_id')->on('id')->references('products');
            $table->timestamps();
        });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question