0

enter image description here

I am using PHP laravel 8.21.0 I have 3 tables. student, grade, and test. When I delete student data, I want all associated grade data to be deleted. Similarly, when I delete test data, I want the associated grade data to be deleted.

I tried using cascade delete, which does not seem to work. Am I doing something wrong? Any better way to make this work? Does anyone have a better idea to structure this relationship...

Grade table Schema:

 public function up()
    {
        Schema::create('grades', function (Blueprint $table) {
            $table->id('id')->unique();
            $table->foreignId('test_id')->constrained()->onDelete('cascade');
            $table->foreignId('student_id')->constrained()->onDelete('cascade');
            $table->date('testDate');
            $table->integer('testCount');
            $table->integer('vocabScore');
            $table->integer('readingScore');
            $table->integer('listeningScore');
            $table->integer('rawTotal');
            $table->timestamps();
        });
    }
Pejman Kheyri
  • 4,044
  • 9
  • 32
  • 39
  • What is your table engine? https://stackoverflow.com/questions/24897300/laravel-foreign-key-ondeletecascade-not-working – Beller Feb 17 '21 at 06:40
  • My table engine is set to null. 'engine' => null I am using mysql database and currently running on localhost – Programming noob Feb 17 '21 at 06:48
  • Check your table engin pls. It wouldn't be null. https://stackoverflow.com/questions/4515490/how-do-i-know-if-a-mysql-table-is-using-myisam-or-innodb-engine – Beller Feb 17 '21 at 06:56
  • I tested your migration code and it works fine to me. You can try to rollback the migration and migrate the table again. – aceraven777 Feb 17 '21 at 06:57
  • try to make the forign key like this : $table->foreign('test_id')->references('id')->on('tests')->onDelete('cascade'); then you should work in innodb database engine like this :- $table->engine = 'InnoDB'; – Mohamed Ahmed Feb 17 '21 at 09:47
  • @MohamedAhmed I think that method is deprecated in laravel 8.21.0 It does not work – Programming noob Feb 18 '21 at 08:02
  • exit in laravel 8 see this docx :- https://laravel.com/docs/master/migrations#database-connection-table-options – Mohamed Ahmed Feb 18 '21 at 08:10

0 Answers0