PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun May 31, 2020 2:11 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Wed Sep 23, 2015 4:05 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
I just created a migration file ,

Syntax: [ Download ] [ Hide ]
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */

    public function up()
    {
        Schema::create('products' , function($table){
            $table->increments('id');
            $table->integer('category_id');
            $table->string('title');
            $table->text('description');
            $table->decimal('height' , 6 , 2);
            $table->decimal('width' , 6 , 2);
            $table->decimal('length' , 6 , 2);
            $table->string('color');
            $table->string('material');
            $table->timestamps();

        });

        Schema::table('products' , function($table){
            $table->foreign('category_id')->references('id')->on('categories');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */

    public function down()
    {
        Schema::drop('products');
    }

}
 


now when i run the command in my CMD ,

Syntax: [ Download ] [ Hide ]
php artisan migrate


I get the following error :

Syntax: [ Download ] [ Hide ]
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter table `products` add constraint products_category_id_foreign foreig n key (`category_id`) references `categories` (`id`)) [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint migrate


So i get the above error , on checking SO for similar issues i got the following

i spit the creation of the migration file, yet i am getting the same error , WHY ? what would be the other reasons causing this error ? can anybody elaborate and explain ?


Top
 Profile  
 
PostPosted: Wed Sep 23, 2015 6:16 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Does categories exist? Are categories.id and products.category_id of the same type? Are the tables populated? Do you have conflicting values?

_________________


Top
 Profile  
 
PostPosted: Wed Sep 23, 2015 7:30 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
before i ran the above migration , i ran a migrations for categories table , the migration file looks like below :

Syntax: [ Download ] [ Hide ]
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategories extends Migration {

        /**
         * Run the migrations.
         *
         * @return void
         */

        public function up()
        {
        Schema::create('categories' , function($table){
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */

        public function down()
        {
        Schema::drop('categories');
        }

}

 


Top
 Profile  
 
PostPosted: Wed Sep 23, 2015 7:30 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
The tables have no values in them , they are empty !


Top
 Profile  
 
PostPosted: Wed Sep 23, 2015 8:05 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Are the related column types exactly the same? Does increments() and integer() have the same default? Check for, say, INT(10) v. INT(11)

_________________


Top
 Profile  
 
PostPosted: Wed Sep 23, 2015 12:10 pm 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
you are right ! one int(10) and the other is int(11) , how do i change that ? :P


Top
 Profile  
 
PostPosted: Wed Sep 23, 2015 12:16 pm 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
$table->integer('post')->length(10);

is the above the correct syntax ?


Top
 Profile  
 
PostPosted: Thu Sep 24, 2015 2:12 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
My migration file looks like so now :

Syntax: [ Download ] [ Hide ]
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration {

    /**
         * Run the migrations.
         *
         * @return void
         */

    public function up()
    {
        Schema::create('products' , function($table){
            $table->increments('id');
            $table->integer('category_id')->length(10);
            $table->string('title');
            $table->text('description');
            $table->decimal('height' , 6 , 2);
            $table->decimal('width' , 6 , 2);
            $table->decimal('length' , 6 , 2);
            $table->string('color');
            $table->string('material');
            $table->timestamps();

        });

        Schema::table('products' , function($table){
            $table->foreign('category_id')->references('id')->on('categories');
        });

    }

    /**
         * Reverse the migrations.
         *
         * @return void
         */

    public function down()
    {
        Schema::drop('products');
    }

}
 


I have changed :

Syntax: [ Download ] [ Hide ]
  $table->integer('category_id')->length(10);
 


I still get the same error though ! :(


Top
 Profile  
 
PostPosted: Thu Sep 24, 2015 3:37 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
Solved it doing:

Syntax: [ Download ] [ Hide ]
      $table->integer('category_id')->unsigned(10);
 


unsigned(10); makes its int(10) , instead of int(11) , magically , i don't know how :P


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group