Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table groups add constraint groups_user_id_foreign foreign key (user_id) references users (id) on delete cascade) #22

Open
dimer22zhc opened this issue Sep 3, 2019 · 1 comment

Comments

@dimer22zhc
Copy link

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table groups add constraint groups_user_id_foreign foreign key (user_id) references users (id) on delete cascade).

I'm getting this issue while trying to migrating _create_groups_tables. Any help plz

@trchristensen
Copy link

trchristensen commented Sep 3, 2019

Replace your create_groups_tables migration file with this
edit: sorry, not sure how to make it neater. Basically, since you're using Laravel 5.8 you need to replace all instances of integer with unsignedBigInteger.

Off topic and maybe the wrong place to post this but I don't see anything or any methods on listing Group data. Only group members, posts, etc. If you figure out how to do this I would appreciate the help. I am still very much a beginner. Thanks.

`<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateGroupsTables extends Migration
{
protected $useBigIncrements;

public function __construct()
{
    $this->useBigIncrements = app()::VERSION >= 5.8;
    
}

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    if ($this->useBigIncrements) {
        Schema::create('groups', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('description')->nullable();
            $table->string('short_description')->nullable();
            $table->string('image')->nullable();
            $table->string('url')->nullable();
            $table->unsignedBigInteger('user_id')->unsignedBigInteger();
            $table->boolean('private')->unsigned()->default(false);
            $table->unsignedBigInteger('conversation_id')->unsignedBigInteger()->nullable();
            $table->text('extra_info')->nullable();
            $table->text('settings')->nullable();
            $table->timestamps();

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

        Schema::create('group_user', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id')->unsignedBigInteger();
            $table->unsignedBigInteger('group_id')->unsignedBigInteger();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('group_id')
                ->references('id')
                ->on('groups')
                ->onDelete('cascade');
        });

        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('body');
            $table->string('type');
            $table->unsignedBigInteger('user_id')->unsignedBigInteger();
            $table->text('extra_info')->nullable();
            $table->timestamps();

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

        Schema::create('comments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('body');
            $table->unsignedBigInteger('user_id')->unsignedBigInteger();
            $table->unsignedBigInteger('post_id')->unsignedBigInteger();
            $table->string('type')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users');

            $table->foreign('post_id')
                ->references('id')
                ->on('posts');
        });

        Schema::create('group_post', function (Blueprint $table) {
            $table->unsignedBigInteger('group_id')->unsignedBigInteger();
            $table->unsignedBigInteger('post_id')->unsignedBigInteger();
            $table->timestamps();

            $table->foreign('group_id')
                ->references('id')
                ->on('groups')
                ->onDelete('cascade');

            $table->foreign('post_id')
                ->references('id')
                ->on('posts')
                ->onDelete('cascade');
        });

        Schema::create('likes', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id')->index();
            $table->unsignedBigInteger('likeable_id')->unsignedBigInteger();
            $table->string('likeable_type');
            $table->primary(['user_id', 'likeable_id', 'likeable_type']);
            $table->timestamps();
        });

        Schema::create('reports', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id')->index();
            $table->unsignedBigInteger('reportable_id')->unsignedBigInteger();
            $table->string('reportable_type');
            $table->primary(['user_id', 'reportable_id', 'reportable_type']);
            $table->timestamps();
        });

        Schema::create('group_request', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id')->unsignedBigInteger()->index();
            $table->unsignedBigInteger('group_id')->unsignedBigInteger()->index();
            $table->timestamps();

            $table->foreign('group_id')
                ->references('id')
                ->on('groups')
                ->onDelete('cascade');

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });
    } else {
        Schema::create('groups', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('description')->nullable();
            $table->string('short_description')->nullable();
            $table->string('image')->nullable();
            $table->string('url')->nullable();
            $table->unsignedBigInteger('user_id')->unsigned();
            $table->boolean('private')->unsigned()->default(false);
            $table->unsignedBigInteger('conversation_id')->unsigned()->nullable();
            $table->text('extra_info')->nullable();
            $table->text('settings')->nullable();
            $table->timestamps();

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

        Schema::create('group_user', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedBigInteger('user_id')->unsigned();
            $table->unsignedBigInteger('group_id')->unsigned();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('group_id')
                ->references('id')
                ->on('groups')
                ->onDelete('cascade');
        });

        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->string('type');
            $table->unsignedBigInteger('user_id')->unsigned();
            $table->text('extra_info')->nullable();
            $table->timestamps();

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

        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->string('body');
            $table->unsignedBigInteger('user_id')->unsigned();
            $table->unsignedBigInteger('post_id')->unsigned();
            $table->string('type')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users');

            $table->foreign('post_id')
                ->references('id')
                ->on('posts');
        });

        Schema::create('group_post', function (Blueprint $table) {
            $table->unsignedBigInteger('group_id')->unsigned();
            $table->unsignedBigInteger('post_id')->unsigned();
            $table->timestamps();

            $table->foreign('group_id')
                ->references('id')
                ->on('groups')
                ->onDelete('cascade');

            $table->foreign('post_id')
                ->references('id')
                ->on('posts')
                ->onDelete('cascade');
        });

        Schema::create('likes', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id')->index();
            $table->unsignedBigInteger('likeable_id')->unsigned();
            $table->string('likeable_type');
            $table->primary(['user_id', 'likeable_id', 'likeable_type']);
            $table->timestamps();
        });

        Schema::create('reports', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id')->index();
            $table->unsignedBigInteger('reportable_id')->unsigned();
            $table->string('reportable_type');
            $table->primary(['user_id', 'reportable_id', 'reportable_type']);
            $table->timestamps();
        });

        Schema::create('group_request', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedBigInteger('user_id')->unsigned()->index();
            $table->unsignedBigInteger('group_id')->unsigned()->index();
            $table->timestamps();

            $table->foreign('group_id')
                ->references('id')
                ->on('groups')
                ->onDelete('cascade');

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

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('groups');
    Schema::drop('group_user');
    Schema::drop('posts');
    Schema::drop('comments');
    Schema::drop('group_post');
    Schema::drop('likes');
    Schema::drop('reports');
    Schema::drop('group_request');
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants