@Terroris337

Как решить ошибку создания связей?

Не могу связать таблици через миграцию выдает ошибку:

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();
        });
  • Вопрос задан
  • 103 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы