2
votes

I am trying to migrate tables created in laravel to database and using foreign key. Buut I am getting the following error. Please help me where is the error?

Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that cor responds to your MariaDB server version for the right syntax to use near ')' at line 1 (SQL: alter table publications add constraint publications_user_id_for eign foreign key (user_id) references registrations ())

Exception trace:

  1. PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 Yo u have an error in your SQL syntax; check the manual that corresponds to your Ma riaDB server version for the right syntax to use near ')' at line 1") C:\xampp\htdocs\plearning\vendor\laravel\framework\src\Illuminate\Database \Connection.php:452

  2. PDO::prepare("alter table publications add constraint publications_user _id_foreign foreign key (user_id) references registrations ()") C:\xampp\htdocs\plearning\vendor\laravel\framework\src\Illuminate\Database \Connection.php:452

Please use the argument -v to see more details.

The parent table is registration and its code is given below.

 <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRegistrationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('registrations', function (Blueprint $table) {
            $table->increments('id'); //primary key
            $table->string('tname');
            $table->string('fname');
            $table->string('domicile');
            $table->integer('nic');
            $table->integer('phone');
            $table->string('email');
            $table->string('off_email');
            $table->string('password');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('registrations');
    }
}

The code of second table where I am using foreign key is educations and its code is given below.

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEducationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('educations', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->refrences('id')->on('registrations');
            $table->string('degree');
            $table->string('univ');
            $table->string('country');
            $table->integer('year');
            $table->text('research_area');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('educations');
    }
}

And the third table where I am using also the foreign key, as a primary key of regstration table is publications and its code is given below.

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePublicationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('publications', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->refrences('id')->on('registrations');
            $table->string('title');
            $table->string('status');
            $table->integer('year');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('publications');
    }
}

The error is above which I already paste. but the publication table is migrating to database, while other two tables registrations and educations is not migrating.

1
$table->foreign('user_id')->references('id')->on('registrations'); your references word is incorrect - Christian Gallarmin

1 Answers

1
votes

I see you have misspelled the word references inside your migrations, could you update it to in all your migrations:

...->references('...')...

If this doesn't fix the issue, then your issue is in the order of execution of migrations, meaning that your file order matters when migrations are ran