1 year ago

Can not add a foreign key

Posted 1 year ago by hjortur17

Hi, i'm trying to add a foreign key in my table but I always get this error message:

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `thread_subscriptions` add constraint `thread_subscriptions_thread_id_foreign` foreign key (`thread_id`) references `threads` (`id`) on delete cascade)

  at /Users/hjorturfreyrlarusson/websites/project/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")

  2   PDOStatement::execute()

  Please use the argument -v to see more details.

Here is my migration:

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


        Schema::table('thread_subscriptions', function($table) {

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