mercredi 24 janvier 2018

Laravel: use primary index as foreign key?

I have a hasOne relation between the model user and the model profilePresi. This means each user has either 0 or 1 president settings. Thus, the primary index of the profilePresi table is identical to the foreign key.

This is how I wanted to add the foreign key:

Schema::create('profilePresi', function (Blueprint $table) {
          $table->integer('idMember');
          $table->primary('idMember');
          $table->string('idCountry',2);
          $table->timestamps();
});

Schema::table('profilePresi', function($table) {
          $table->foreign('idMember')
                ->references('id')->on('users')
                ->onDelete('cascade');
});

However I get the following error:

enter image description here

How can I fix it? I am using InnoDB engine so I know that I am allowed to set index key as foreign key.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire