<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problem on migration : products category foreign key]]></title><description><![CDATA[<p dir="auto"><strong>on laravel8 framework , can anyone help me with this i also used ,</strong></p>
<p dir="auto">this way but still it doesn't work<br />
<a href="https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint</a></p>
<p dir="auto"><strong>Cannot add foreign key constraint..</strong></p>
<pre><code>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-&gt;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></pre>
<p dir="auto">code on the migration</p>
<pre><code>&lt;?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-&gt;id();
            $table-&gt;string('name');
            $table-&gt;string('slug')-&gt;unique();
            $table-&gt;string('Short_Description')-&gt;nullable();
            $table-&gt;string('description');
            $table-&gt;decimal('regular_price');
            $table-&gt;decimal('sale_price')-&gt;nullable();
            $table-&gt;string('SKU');
            $table-&gt;enum('stock_status',['instock','outstock']);
            $table-&gt;boolean('Featured')-&gt;default(false);
            $table-&gt;unsignedInteger('quantity')-&gt;default(10);
            $table-&gt;string('image')-&gt;nullable();
            $table-&gt;string('images')-&gt;nullable();
            $table-&gt;integer('category_id')-&gt;nullable()-&gt;unsigned();
            $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onDelete('cascade');
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

</code></pre>
]]></description><link>https://lankadevelopers.lk/topic/782/problem-on-migration-products-category-foreign-key</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Jul 2026 14:15:53 GMT</lastBuildDate><atom:link href="https://lankadevelopers.lk/topic/782.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 19 Jan 2021 10:02:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Thu, 21 Jan 2021 14:45:54 GMT]]></title><description><![CDATA[<p dir="auto">try with this</p>
<pre><code>$table-&gt;unsignedBigInteger('category_id')-&gt;nullable();
$table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories');
</code></pre>
]]></description><link>https://lankadevelopers.lk/post/4301</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4301</guid><dc:creator><![CDATA[root]]></dc:creator><pubDate>Thu, 21 Jan 2021 14:45:54 GMT</pubDate></item><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Thu, 21 Jan 2021 12:56:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/1616">@kasun-rathnayake</a> can you post the categories table migration?</p>
]]></description><link>https://lankadevelopers.lk/post/4300</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4300</guid><dc:creator><![CDATA[dev_lak]]></dc:creator><pubDate>Thu, 21 Jan 2021 12:56:09 GMT</pubDate></item><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Thu, 21 Jan 2021 12:40:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/2">@dev_lak</a> 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.</p>
]]></description><link>https://lankadevelopers.lk/post/4299</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4299</guid><dc:creator><![CDATA[Kasun Rathnayake]]></dc:creator><pubDate>Thu, 21 Jan 2021 12:40:54 GMT</pubDate></item><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Thu, 21 Jan 2021 12:39:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/27">@root</a> said in <a href="/post/4271">Problem on migration : products category foreign key</a>:</p>
<blockquote>
<p dir="auto">show me your database/migrations folder</p>
</blockquote>
<p dir="auto"><img src="/assets/uploads/files/1611232749107-screenshot-2021-01-21-at-8.36.08-pm.png" alt="Screenshot 2021-01-21 at 8.36.08 PM.png" class=" img-responsive img-markdown" /></p>
]]></description><link>https://lankadevelopers.lk/post/4298</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4298</guid><dc:creator><![CDATA[Kasun Rathnayake]]></dc:creator><pubDate>Thu, 21 Jan 2021 12:39:12 GMT</pubDate></item><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Thu, 21 Jan 2021 12:35:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/8">@nubelle</a> did it bro still the same</p>
]]></description><link>https://lankadevelopers.lk/post/4297</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4297</guid><dc:creator><![CDATA[Kasun Rathnayake]]></dc:creator><pubDate>Thu, 21 Jan 2021 12:35:28 GMT</pubDate></item><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Wed, 20 Jan 2021 18:34:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/1616">@kasun-rathnayake</a> change data type of category_id as unsignedBigInteger()</p>
]]></description><link>https://lankadevelopers.lk/post/4286</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4286</guid><dc:creator><![CDATA[Nubelle]]></dc:creator><pubDate>Wed, 20 Jan 2021 18:34:19 GMT</pubDate></item><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Tue, 19 Jan 2021 10:31:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/1616">@kasun-rathnayake</a> Possible Causes</p>
<ol>
<li>Data type on category_id column and id column on category table mismatch</li>
<li>Check migration order, category table should created first prior to products table</li>
</ol>
]]></description><link>https://lankadevelopers.lk/post/4272</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4272</guid><dc:creator><![CDATA[dev_lak]]></dc:creator><pubDate>Tue, 19 Jan 2021 10:31:18 GMT</pubDate></item><item><title><![CDATA[Reply to Problem on migration : products category foreign key on Tue, 19 Jan 2021 10:12:40 GMT]]></title><description><![CDATA[<p dir="auto">show me your database/migrations folder</p>
]]></description><link>https://lankadevelopers.lk/post/4271</link><guid isPermaLink="true">https://lankadevelopers.lk/post/4271</guid><dc:creator><![CDATA[root]]></dc:creator><pubDate>Tue, 19 Jan 2021 10:12:40 GMT</pubDate></item></channel></rss>