B
B
BonBon Slick2017-03-05 23:01:52
MySQL
BonBon Slick, 2017-03-05 23:01:52

Cannot add foreign key constraint?

I'm trying to create links between users and subscriptions.
The order in which the tables were created is correct. First users, then subscriptions.
Subscription migration

Schema::create('subscriptions', function ($table) {
           $table->engine = 'InnoDB';

           $table->increments('id');
           $table->integer('user_id')->unsigned();
           $table->string('name');
           $table->string('stripe_id');
           $table->string('stripe_plan');
           $table->integer('quantity');
           $table->timestamp('trial_ends_at')->nullable();
           $table->timestamp('ends_at')->nullable();
           $table->timestamps();

           $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
       });

// возможно это поможет, этот участок тоже не работат
 public function down()
      {
         Schema::dropIfExists('subscriptions');
 // migrate:refresh не работает, выдает что таблица уже есть хотя эта функция должна ее сбрасывать.
     }

Mistake
[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL:
   alter table `subscriptions` add constraint `subscriptions_user_id_foreign`
  foreign key (`user_id`) references `users` (`id`) on delete set null)



  [PDOException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

As you can see, it prohibits creating a connection.
Tried:
1 - recreate migrations
2 - add InnoDB

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question