Each course on my DB has a correspondent faculty_id.
Each faculty has affinities with other faculties (and as a consequense so do the courses with those faculty_ids). Faculty affinities and courses affinities are sotred y sepparated tables.
I'm trying to build a trigger which deletes records from the course_affinity table when a record on faculty_affinity is deleted.
This is what I have. It does not work.
CREATE TRIGGER deleteCourses
AFTER DELETE
ON faculty_affinities
AS
BEGIN
DELETE FROM course_affinities
JOIN courses AS course1
JOIN courses AS course2
ON course_affinities.course1_id = course1.id
AND course_affinities.course2_id = course2.id
WHERE (course1.faculty_id = DELETED.faculty1_id OR course1.faculty_id = DELETED.faculty2_id)
AND (course2.faculty_id = DELETED.faculty1_id OR course2.faculty_id = DELETED.faculty2_id);
END;
Help someone? What am I doing wrong?
Edit: I get error
SQLSTATE[4200]: Syntax error... neas 'AS at line 4
Edit2: my migrations
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 150);
$table->integer('faculty_id')->unsigned();
$table->foreign('faculty_id')->references('id')->on('faculties');
$table->boolean('active')->default(1);
//$table->softDeletes();
});
Schema::create('faculties', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 150)->unique();
$table->boolean('active')->default(1);
//$table->softDeletes();
});
Schema::create('course_affinities', function (Blueprint $table) {
$table->increments('id');
$table->integer('course1_id')->unsigned();
$table->foreign('course1_id')->references('id')->on('courses');
$table->integer('course2_id')->unsigned();
$table->foreign('course2_id')->references('id')->on('courses');
$table->boolean('active')->default(1);
//$table->softDeletes();
});
Schema::create('faculty_affinities', function (Blueprint $table) {
$table->increments('id');
$table->integer('faculty1_id')->unsigned();
$table->foreign('faculty1_id')->references('id')->on('faculties');
$table->integer('faculty2_id')->unsigned();
$table->foreign('faculty2_id')->references('id')->on('faculties');
$table->boolean('active')->default(1);
//$table->softDeletes();
});