Answer the question
In order to leave comments, you need to log in
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 не работает, выдает что таблица уже есть хотя эта функция должна ее сбрасывать.
}
[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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question