[英]Foreign key constraing migration on Laravel 8 with syntax error
我正在嘗試在 MySQL 8.0.21 - MySQL 社區服務器上執行一些 Laravel 8 Migrations,並嘗試使用一些外鍵關系,但遷移失敗
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1")
我收到的第二個錯誤聲明是:
PDO::prepare("alter table `user_course` add constraint `user_course_user_id_foreign` foreign key (`user_id`) references `users` ()")
將在關系遷移中使用的第一個表是 users 表:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->text('profile_photo_path')->nullable();
$table->timestamps();
});
}
第二個是課程表:
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->tinyInteger('qnt_lessons');
$table->smallInteger('qnt_hours');
$table->string('link');
$table->string('img')->nullable();
$table->string('sell_link')->nullable();
$table->timestamps();
});
}
失敗的遷移是這個:
public function up()
{
Schema::create('user_course', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->reference('id')->on('users');
$table->unsignedBigInteger('course_id');
$table->foreign('course_id')->reference('id')->on('courses');
$table->tinyInteger('complete_lessons')->default(0);
$table->boolean('completed');
$table->date('due_date');
$table->string('buy_platform');
$table->string('buy_status', 32)->nullable();
$table->timestamps();
});
}
問題出在語法上,你把參考和它的參考放在這里:
$table->foreign('user_id')->reference('id')->on('users');
和這里:
$table->foreign('course_id')->reference('id')->on('courses');
例如,您的代碼如下
$table->foreign('course_id')->references('id')->on('courses');
我給你留下了 laravel 的文檔,所以你可以查看
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
問候。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.