0
votes

I'm having trouble with Laravel database migration. I have entered foreign key constraint in my database migration file, but when I try to migrate the file it shows this error message.

Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') on delete cascade on update cascade' at line 1 (SQL: alter table education_qualifications add constraint education_qualifications_teacher_id_foreign foreign key (teacher_id) references teachers () on delete cascade on update cascade)at E:\XAMPP\htdocs\ViduresaApp\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|         }
    668|

Exception trace:

1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') on delete cascade on update cascade' at line 1") E:\XAMPP\htdocs\ViduresaApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452

2 PDO::prepare("alter table education_qualifications add constraint education_qualifications_teacher_id_foreign foreign key (teacher_id) references teachers () on delete cascade on update cascade") E:\XAMPP\htdocs\ViduresaApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452

<?php

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

class CreateEducationQualificationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('education_qualifications', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('teacher_id')->nullable();
            $table->unsignedInteger('student_id')->nullable();
            $table->string('institute_name');
            $table->string('user_degree');
            $table->string('field_of_study');
            $table->string('user_grade');
            $table->date('from_date')->nullable();
            $table->date('to_date')->nullable();
            $table->text('edu_description');
            $table->timestamps();

            $table->foreign('teacher_id')->references('id')->on('teachers')->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('student_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');

            $table->primary(['teacher_id', 'student_id']);

        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('education_qualifications');
    }
}
1
Please can you shoe the migration file that contains the table the error is talking about (education_qualifications)?Rwd
Sorry I had added the wrong code above. Now I have updated it with correct code (migration file that contains the table).Sanka Harshana

1 Answers

0
votes

You can use both onupdate and ondelete at the same line

For example:

$table->foreign(’author’)->references(’id’)->on(’users’)->onUpdate(’cascade’);