【发布时间】:2019-07-03 17:23:42
【问题描述】:
我在进行 Laravel 数据库迁移时遇到问题。我在我的数据库迁移文件中输入了外键约束,但是当我尝试迁移文件时,它显示了这个错误消息。
Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行(SQL:alter table
education_qualificationsadd constrainteducation_qualifications_teacher_id_foreignforeign key (teacher_id) 参考中的 delete cascade on update cascade' 附近使用正确的语法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|
异常跟踪:
1 PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MariaDB 服务器版本相对应的手册,了解在 ') 附近使用的正确语法在第 1 行更新级联时删除级联") E:\XAMPP\htdocs\ViduresaApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452
2 PDO::prepare("修改表
education_qualifications添加约束education_qualifications_teacher_id_foreign外键(teacher_id)引用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');
}
}
【问题讨论】:
-
请您提供包含错误所指表的迁移文件 (
education_qualifications)? -
对不起,我在上面添加了错误的代码。现在我用正确的代码(包含表的迁移文件)更新了它。
标签: database laravel migration