php - Laravel 6.5.1 migration Errno: Syntax error or access violation 1064

I tried to do the migration and I ran into this error: Syntax error or access violation 1064. I looked at other similar problems but I couldn't find the solution to this problem. Please help, and thanks in advance.
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('author_id');
$table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
$table->string('title');
$table->text('excerpt');
$table->longText('body');
$table->binary('post_image');
$table->timestamp('published_at')->nullable();
$table->timestamps();
$table->softDeletes();
});
Schema::create('authors', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('fist_name');
$table->string('last_name');
$table->binary('pic')->nullable();
$table->timestamps();
$table->softDeletes();
});
Answer
Solution:
Assuming that this is one single migration creating both tables. Which would, in my opinion, not the best idea. Better to separate each table into its own migration.
Try to switch both of the creation blocks. So that before anything about theposts
-table is done theauthors
-table is already created.
Because you are trying to set a foreign key from theposts
-table to not yet createdauthors
-table but both of them have to exist.
I also tend to separate foreign key constraints from the actual structure of a table for a better overview. But this would be thing of personal opinion.
Schema::create('authors', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('fist_name');
$table->string('last_name');
$table->binary('pic')->nullable();
$table->timestamps();
$table->softDeletes();
});
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('author_id');
$table->string('title');
$table->text('excerpt');
$table->longText('body');
$table->binary('post_image');
$table->timestamp('published_at')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
});
Hope that this can help a bit.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function getclientoriginalname() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.