Laravelの移行で参照エラーが発生するのはなぜですか?

ロスカリー:

私はLaravel eコマースサイトを作成しており、このチュートリアルに従っています:https : //www.youtube.com/watch?v =0lo7vzO1Fto&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=20&t=417s

エピソード18で、Orderテーブルが作成されます。その最初のセクションで苦労して、マイグレーションを作成しています。2つの作成移行ファイルがあります。

  1. 2020_07_10_134530_create_orders_table.php(注文テーブルを作成します)

  2. 2020_07_10_135517_create_order_product_table.php(注文と商品のピボットテーブルを作成します)

私の「create_orders_table.php」は次のようになります。

 public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('users')
                ->onUpdate('cascade')->onDelete('set null');
            $table->string('billing_email')->nullable();
            $table->string('billing_name')->nullable();
            $table->string('billing_address')->nullable();
            $table->string('billing_city')->nullable();
            $table->string('billing_province')->nullable();
            $table->string('billing_postalcode')->nullable();
            $table->string('billing_phone')->nullable();
            $table->string('billing_name_on_card')->nullable();
            $table->integer('billing_discount')->default(0);
            $table->string('billing_discount_code')->nullable();
            $table->integer('billing_subtotal');
            $table->integer('billing_tax');
            $table->integer('billing_total');
            $table->string('payment_gateway')->default('stripe');
            $table->boolean('shipped')->default(false);
            $table->string('error')->nullable();
            $table->timestamps();
        });
    }

そして、私の「create_order_product_table.php」は次のようになります。

public function up()
    {
        Schema::create('order_product', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('order_id')->unsigned()->nullable();
            $table->foreign('order_id')->references('id')
                ->on('orders')->onUpdate('cascade')->onDelete('set null');

            $table->integer('product_id')->unsigned()->nullable();
            $table->foreign('product_id')->references('id')
                ->on('iamlushes')->onUpdate('cascade')->onDelete('set null');

            $table->integer('quantity')->unsigned();
            $table->timestamps();
        });
    }

コマンドを実行すると:

php artisan migrate

端末から次のエラーが表示されます。

rosscurrie@Rosss-Air JanVal % php artisan migrate
Migrating: 2020_07_10_134530_create_orders_table

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible. (SQL: alter table `orders` add constraint `orders_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete set null on update cascade)

  at 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| 

      +11 vendor frames 
  12  database/migrations/2020_07_10_134530_create_orders_table.php:38
      Illuminate\Support\Facades\Facade::__callStatic("create")

      +22 vendor frames 
  35  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

主なエラーは次のようです:

SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible. (SQL: alter table `orders` add constraint `orders_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete set null on update cascade)

そして、それは私の 'create_orders_table.php'移行でこのコード行を参照していると思います:

$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');

私はこのエラーを修正する方法がわかりません、これが私の '2014_10_12_000000_create_users_table.php'移行です:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

そして、これが私の製品テーブル「2020_06_16_124046_create_iamlushes_table.php」の移行です。

public function up()
    {
        Schema::create('iamlushes', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->string('fullname')->unique();
            $table->string('productLogo');
            $table->string('img')->unique();
            $table->text('description');
            $table->integer('price');
            $table->timestamps();
        });
    }
BABAK ASHRAFI:

テーブルの$table->id();作成に使用していusersます。データベースにunsinedBigInteger型の列を作成しますが、ordersテーブル内のこの列unsignedIntegerを、競合を引き起こすとして参照しています

あなたは使用する必要がありますunsignedBigIntegerのための列の型としてuser_idではorders下記のように、テーブル:

Schema::create('orders', function (Blueprint $table) {
        $table->increments('id');
        $table->bigInteger('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users')
            ->onUpdate('cascade')->onDelete('set null');
        
        ....your code
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

DataGridViewでNull参照エラーが発生するのはなぜですか

分類Dev

javascript参照エラーが発生するのはなぜですか?

分類Dev

Laravelで「Undefinedvariable:var」エラーが発生するのはなぜですか?

分類Dev

lValueエラーが発生するのはなぜですか?

分類Dev

ElementClickInterceptedExceptionエラーが発生するのはなぜですか?

分類Dev

「sshdBadprimedescription」エラーが発生するのはなぜですか?

分類Dev

値エラーが発生するのはなぜですか

分類Dev

値エラーが発生するのはなぜですか

分類Dev

エラーが発生するのはなぜですか?

分類Dev

「ArrayIndexOutOfBoundsException」エラーが発生するのはなぜですか?

分類Dev

NoMessageBodyWriterFoundFailureエラーが発生するのはなぜですか?

分類Dev

「AttributeError」エラーが発生するのはなぜですか

分類Dev

'operator ++'を使用した参照の初期化でエラーが発生するのはなぜですか

分類Dev

参照をリセットするときにエラーが発生しないのはなぜですか?

分類Dev

例外ではなくエラーが発生するのはなぜですか?

分類Dev

この「未定義の参照」エラーが発生し続けるのはなぜですか?

分類Dev

「robots :: robots()」への未定義の参照というエラーが発生し続けるのはなぜですか

分類Dev

LaravelでAgGridでSassを使用するとエラーが発生するのはなぜですか?

分類Dev

SVNからGITへの移行で、共通の参照がないためエラーが発生する

分類Dev

SQL クエリでエラーが発生するのはなぜですか?

分類Dev

ERBでこのエラーが発生するのはなぜですか?

分類Dev

openCVのfastNlMeansDenoisingColored()でエラーが発生するのはなぜですか?

分類Dev

CircieCIでのみwebpackerエラーが発生するのはなぜですか?

分類Dev

TransactionScopeの使用でエラーが発生するのはなぜですか?

分類Dev

Glimpseでこのエラーが発生するのはなぜですか?

分類Dev

Glimpseでこのエラーが発生するのはなぜですか?

分類Dev

このエラーがcで発生するのはなぜですか?

分類Dev

SQL Serverでこのエラーが発生するのはなぜですか?

分類Dev

PL / SQLでこのエラーが発生するのはなぜですか

Related 関連記事

  1. 1

    DataGridViewでNull参照エラーが発生するのはなぜですか

  2. 2

    javascript参照エラーが発生するのはなぜですか?

  3. 3

    Laravelで「Undefinedvariable:var」エラーが発生するのはなぜですか?

  4. 4

    lValueエラーが発生するのはなぜですか?

  5. 5

    ElementClickInterceptedExceptionエラーが発生するのはなぜですか?

  6. 6

    「sshdBadprimedescription」エラーが発生するのはなぜですか?

  7. 7

    値エラーが発生するのはなぜですか

  8. 8

    値エラーが発生するのはなぜですか

  9. 9

    エラーが発生するのはなぜですか?

  10. 10

    「ArrayIndexOutOfBoundsException」エラーが発生するのはなぜですか?

  11. 11

    NoMessageBodyWriterFoundFailureエラーが発生するのはなぜですか?

  12. 12

    「AttributeError」エラーが発生するのはなぜですか

  13. 13

    'operator ++'を使用した参照の初期化でエラーが発生するのはなぜですか

  14. 14

    参照をリセットするときにエラーが発生しないのはなぜですか?

  15. 15

    例外ではなくエラーが発生するのはなぜですか?

  16. 16

    この「未定義の参照」エラーが発生し続けるのはなぜですか?

  17. 17

    「robots :: robots()」への未定義の参照というエラーが発生し続けるのはなぜですか

  18. 18

    LaravelでAgGridでSassを使用するとエラーが発生するのはなぜですか?

  19. 19

    SVNからGITへの移行で、共通の参照がないためエラーが発生する

  20. 20

    SQL クエリでエラーが発生するのはなぜですか?

  21. 21

    ERBでこのエラーが発生するのはなぜですか?

  22. 22

    openCVのfastNlMeansDenoisingColored()でエラーが発生するのはなぜですか?

  23. 23

    CircieCIでのみwebpackerエラーが発生するのはなぜですか?

  24. 24

    TransactionScopeの使用でエラーが発生するのはなぜですか?

  25. 25

    Glimpseでこのエラーが発生するのはなぜですか?

  26. 26

    Glimpseでこのエラーが発生するのはなぜですか?

  27. 27

    このエラーがcで発生するのはなぜですか?

  28. 28

    SQL Serverでこのエラーが発生するのはなぜですか?

  29. 29

    PL / SQLでこのエラーが発生するのはなぜですか

ホットタグ

アーカイブ