littleboby
4 months ago

Illuminate\Database\QueryException : SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

Posted 4 months ago by littleboby

Creating an application where people create events or 'meetups'. I created my factories: 2019_06_20162119_create_groups_table, (different meetup categories) 201906_20_170900create_meetups_table, (the events themselves) 201906_20182232_create_attending_table, (people that are attending) & 201906_20_182319_create_group_members_table (members of the different categories)

After I created the seeders and factories and doing php artisan db:seed the error comes up, here's what my scripts look like(sorry for the long post):

Meetups table:

class CreateMeetupsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('meetups', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->string('title');
           $table->longText('description');
           $table->string('place');
           $table->dateTimeTz('time_happening');
           $table->timestamps();
           $table->bigInteger('created_by')->unsigned();
           $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
           $table->bigInteger('group_id')->unsigned();
           $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists('meetups');
   }
}

Groups table:

class CreateGroupsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('groups', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('description');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('groups');
    }
}

Attending table:

class CreateAttendingTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('attending', function (Blueprint $table) {
            $table->bigInteger('meetup_id')->unsigned();
            $table->foreign('meetup_id')->references('id')->on('meetups')->onDelete('cascade');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('attending');
    }
}

Group members table:

class CreateGroupMembersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('group_members', function (Blueprint $table) {
            $table->bigInteger('group_id')->unsigned();
            $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('group_members');
    }
}

MeetupsTableSeeder:

class MeetupsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Meetup::class, 100)->create();
    }
}

AttendingTableSeeder:

class AttendingTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Attending::class, 100)->create();
    }
}

GroupMembersSeeder

class GroupMembersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\GroupMembers::class, 500)->create();
    }
}

UserTableSeeder:

public function run()
    {
        factory(App\User::class, 50)->create();
    }

& DatabaseSeeder

public function run()
    {
        $this->call(MeetupsTableSeeder::class);
        $this->call(UserTableSeeder::class);
        $this->call(AttendingTableSeeder::class);
        $this->call(GroupMembersSeederTableSeeder::class);
    }

The meetups factory looks like this:

$factory->define(App\Meetup::class, function (Faker $faker) {
    return [
        'title' => $faker->text(30),
        'description' => $faker->text(200),
        'place' => $faker->streetAddress(),
        'created_by' => rand(1, 50),
        'group_id' => rand(1, 15),
        'time_happening' =>$faker->dateTime($max = 'now', $timezone = null)
    ];
});

Group members factory:

$factory->define(App\GroupMembers::class, function (Faker $faker) {
    return [
        'group_id' => rand(1, 15),
        'user_id' => rand(1, 50)
    ];
});

Attending factory:

$factory->define(App\Attending::class, function (Faker $faker) {
    return [
        'meetup_id' => rand(1, 100),
        'user_id' => rand(1, 50)
    ];
});

& the Users factory:

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => bcrypt('secret'), // secret
        'remember_token' => Str::random(10),
    ];
});

I also created the models for every table/class but have written nothing in them as yet, I am aware that there are a lot of questions like this on other platforms but none of the answers seem to help with my case, so in advance thank you kind stranger/s!

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