jsrosas
3 years ago

Adding Foreign Keys after creating all tables.

Posted 3 years ago by jsrosas

Hiya all, I created some tables and now I would like to add my key constrains after all tables have been created, so I am doing a new migration file with instructions for all foreign keys to be created, even though its running after all the tables have been created I get:

SQLSTATE[HY000]: General error: 1005 Can't create table 'localtest.#sql-62c_41c6' (errno: 150) (SQL: alter table >member_payments add constraint member_payments_member_id_foreign foreign key (member_id) references >members (id))

The migration file looks like this:

public function up()
   {
       Schema::table('users', function (Blueprint $table){
           $table->foreign('member_id')->references('id')->on('members');
       });
       Schema::table('member_payments', function (Blueprint $table){
           $table->foreign('member_id')->references('id')->on('members');
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::table('users', function (Blueprint $table) {
           $table->dropForeign(['member_id']);
       });
       Schema::table('member_payments', function (Blueprint $table) {
           $table->dropForeign(['member_id']);
       });
   }

Any ideas why its generating this error? thank you in advance.

Please sign in or create an account to participate in this conversation.