我在与Laravel数据库迁移的麻烦。我在我的数据库迁移文件中输入外键约束,但是当我尝试迁移文件时,它显示了这个错误消息。
照亮\数据库\ QueryException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误;检查对应于您MariaDB的服务器版本,在1号线“上更新级联删除级联)”使用附近的正确语法手册(SQL:ALTER TABLE
education_qualifications
添加约束education_qualifications_teacher_id_foreign
外键(teacher_id
)上删除级联引用teachers
()在E处级联更新):\ XAMPP \ htdocs中\ ViduresaApp \供应商\ laravel \框架的\ src \照亮\数据库\ 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|
异常跟踪:
1个PDOException::(“SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误;检查对应于您的MariaDB的服务器版本上删除梯级利用靠近“)正确的语法手册更新在第1" 行)电子级联”:\ XAMPP \ htdocs中\ ViduresaApp \厂商\ laravel \框架\ SRC \照亮\数据库\ Connection.php:452
2 PDO ::准备( “改变表
education_qualifications
添加约束education_qualifications_teacher_id_foreign
外键(teacher_id
)上更新级联删除级联引用teachers
()”)E:\ XAMPP \ htdocs中\ ViduresaApp \供应商\ laravel \框架的\ src \照亮\数据库\ 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');
}
}
您可以在同一条线上同时使用的onupdate和ondelete
例如:
$table->foreign(’author’)->references(’id’)->on(’users’)->onUpdate(’cascade’);