Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    Problem on migration : products category foreign key

    Laravel Framework
    laravel
    4
    9
    725
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Kasun Rathnayake
      Kasun Rathnayake Web Development last edited by

      on laravel8 framework , can anyone help me with this i also used ,

      this way but still it doesn't work
      https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint

      Cannot add foreign key constraint..

      Error is as below
      
      SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `products` add constraint `products_category_id_foreign` foreign key (`category_id`) references `categories` (`id`))
      
        at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
          674▕         // If an exception occurs when attempting to run a query, we'll format the error
          675▕         // message to include the bindings with SQL, which will make this exception a
          676▕         // lot more helpful to the developer instead of just the database's errors.
          677▕         catch (Exception $e) {
        ➜ 678▕             throw new QueryException(
          679▕                 $query, $this->prepareBindings($bindings), $e
          680▕             );
          681▕         }
          682▕
      
            +9 vendor frames
        10  database/migrations/2021_01_19_074944_create_products_table.php:38
            Illuminate\Support\Facades\Facade::__callStatic("create")
      
            +21 vendor frames
        32  artisan:37
            Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      
      

      code on the migration

      <?php
      
      use Illuminate\Database\Migrations\Migration;
      use Illuminate\Database\Schema\Blueprint;
      use Illuminate\Support\Facades\Schema;
      
      class CreateProductsTable extends Migration
      {
          /**
           * Run the migrations.
           *
           * @return void
           */
          public function up()
          {
              Schema::create('products', function (Blueprint $table) {
                  $table->id();
                  $table->string('name');
                  $table->string('slug')->unique();
                  $table->string('Short_Description')->nullable();
                  $table->string('description');
                  $table->decimal('regular_price');
                  $table->decimal('sale_price')->nullable();
                  $table->string('SKU');
                  $table->enum('stock_status',['instock','outstock']);
                  $table->boolean('Featured')->default(false);
                  $table->unsignedInteger('quantity')->default(10);
                  $table->string('image')->nullable();
                  $table->string('images')->nullable();
                  $table->integer('category_id')->nullable()->unsigned();
                  $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
                  $table->timestamps();
              });
          }
      
          /**
           * Reverse the migrations.
           *
           * @return void
           */
          public function down()
          {
              Schema::dropIfExists('products');
          }
      }
      
      
      dev_lak Nubelle 2 Replies Last reply Reply Quote 1
      • root
        root Linux Help last edited by

        show me your database/migrations folder

        Kasun Rathnayake 1 Reply Last reply Reply Quote 0
        • dev_lak
          dev_lak @Kasun Rathnayake last edited by

          @kasun-rathnayake Possible Causes

          1. Data type on category_id column and id column on category table mismatch
          2. Check migration order, category table should created first prior to products table
          Kasun Rathnayake 1 Reply Last reply Reply Quote 0
          • Nubelle
            Nubelle Web Development @Kasun Rathnayake last edited by

            @kasun-rathnayake change data type of category_id as unsignedBigInteger()

            Kasun Rathnayake 1 Reply Last reply Reply Quote 0
            • Kasun Rathnayake
              Kasun Rathnayake Web Development @Nubelle last edited by

              @nubelle did it bro still the same

              1 Reply Last reply Reply Quote 0
              • Kasun Rathnayake
                Kasun Rathnayake Web Development @root last edited by Kasun Rathnayake

                @root said in Problem on migration : products category foreign key:

                show me your database/migrations folder

                Screenshot 2021-01-21 at 8.36.08 PM.png

                1 Reply Last reply Reply Quote 0
                • Kasun Rathnayake
                  Kasun Rathnayake Web Development @dev_lak last edited by

                  @dev_lak thanks bro i checked them too , but not working as said on the forums , its like sql dont get foreign keys , but also i tried out there solutions too but not working.

                  dev_lak 1 Reply Last reply Reply Quote 0
                  • dev_lak
                    dev_lak @Kasun Rathnayake last edited by

                    @kasun-rathnayake can you post the categories table migration?

                    1 Reply Last reply Reply Quote 0
                    • root
                      root Linux Help last edited by

                      try with this

                      $table->unsignedBigInteger('category_id')->nullable();
                      $table->foreign('category_id')->references('id')->on('categories');
                      
                      1 Reply Last reply Reply Quote 1
                      • 1 / 1
                      • First post
                        Last post

                      2
                      Online

                      3.6k
                      Users

                      1.3k
                      Topics

                      5.3k
                      Posts

                      • Privacy
                      • Terms & Conditions
                      • Donate

                      © Copyrights and All right reserved Lanka Developers Community

                      Powered by Axis Technologies (PVT) Ltd

                      Made with in Sri Lanka

                      | |