Skip to content
Advertisement

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();
        });

Advertisement

Answer

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 the posts-table is done the authors-table is already created.

Because you are trying to set a foreign key from the posts-table to not yet created authors-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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement