Reputation: 101
I'm trying to execute some Laravel 8 Migatrions on MySQL 8.0.21 - MySQL Community Server, and trying to use some foreign key relationship, but the migration fails with
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")
The second error statement I receive is:
PDO::prepare("alter table `user_course` add constraint `user_course_user_id_foreign` foreign key (`user_id`) references `users` ()")
The first table that will be used in the relationship migration it's the users table:
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();
});
}
The second one is the courses table:
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();
});
}
And the migration that fails is this one:
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();
});
}
Upvotes: 0
Views: 606
Reputation: 79
The problem is in the syntax, you put reference and it's references here:
$table->foreign('user_id')->reference('id')->on('users');
And here:
$table->foreign('course_id')->reference('id')->on('courses');
For example, your code would be as follows
$table->foreign('course_id')->references('id')->on('courses');
I leave you the documentation of laravel so you can review
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
Regards.
Upvotes: 1