When I run migrations to create foreign key constraints, I get the following error on the command line. I need help to overcome this, since I searched a lot on the Internet and tried different things, but, unfortunately, no one worked.
In the line Connection.php 647:
SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error i n your SQL syntax; check the manual that matches your MariaDB server version for the correct syntax to use next to ') when deleting a cascade' in line 1 (SQL: alter table category_posts
add constraint category_posts_post_id_fo
reign
foreign key ( post_id
) links posts
() when deleting a cascade)
On line 445 Connection.php:
SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error i n your SQL syntax; check the manual that matches your MariaDB server version for the correct syntax to use next to ') when deleting the cascade' on line 1
My migration code to restrict the foreign key is as follows
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoryPostsTable extends Migration
{
public function up()
{
Schema::create('category_posts', function (Blueprint $table) {
$table->integer('category_id')->unsigned()->index();
$table->integer('post_id')->unsigned()->index();
$table->foreign('post_id')->referances('id')->on('posts')->onDelete('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('category_posts');
}
}
Please help me. Thanks you
source
share