jeudi 19 novembre 2015

adding foreign key to the tables in laravel 5.1

I've spent too much time to figure out why migrate doesn't work.I almost read all posts and questions in this context but I couldn't find a solution .

I want to create tables which some columns are foreign key. I tested if I hadn't foreign key all tables would create without any errors. this is my first migration:

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_info_tbl', function ( $table) {
            $table->engine = 'InnoDB';
            $table->increments('userID',true);
            $table->integer('guID')->unsigned();
            $table->boolean('isRegister');
            $table->string('carrierName');
            $table->string('city');
            $table->string('resolution');
            $table->string('osVersion');
            $table->string('ram');
            $table->string('macAddress')->unique();
            $table->rememberToken();
            $table->timestamps();
        });



    }

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

second migration :

class CreateAdsInfoTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ads_info_tbl', function ( $table) {
            $table->engine = 'InnoDB';
            $table->increments('adsID',true);
            $table->integer('userID')->unsigned();
            $table->boolean('isValid');
            $table->integer('cost');
            $table->string('description');
            $table->string('title');
            $table->timestamp('createAt');
            $table->rememberToken();
            $table->timestamps();
        });

        Schema::table('ads_info_tbl', function ( $table) {
            $table->engine = 'InnoDB';
            $table->foreign('userID')->references('userID')->on('user_info_tbl');
        });
        }

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

when I remove third migration all tables create correctly but after adding third migration error comes:

>php artisan migrate
Migration table created successfully.



  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'adsdatabase3.#sql-8e8_2c' (errno: 150) (SQL: alter table `
  user_reg_tbl` add constraint user_reg_tbl_guid_foreign foreign key (`guID`) references `user_info_tbl` (`guID`))


  [PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'adsdatabase3.#sql-8e8_2c' (errno: 150)

this is my third migration :

    class CreatRegistration extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_reg_tbl', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('userRegID');
            $table->integer('guID');
            $table->string('name');
            $table->string('lastName');
            $table->string('password');
            $table->string('email');
            $table->boolean('isAdmin');
            $table->boolean('isSuperAdmin');
            $table->timestamps();
            $table->foreign('guID')->unsigned()->references('guID')->on('user_info_tbl');
        });
    }

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

I've tried different options like adding INNODB engine to all my tables. Or I had read somewhere that we must add unsigned() to avoid these errors. something is too annoying all table create in mysql otherwise I get error!. but when I want to delete the table that error related to ,mysql error comes : unknown table, and I had to delete whole database and renew one. I don't know what is wrong.

I'm using Laravel 5.1 in phpStorm environment.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire