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
publicationsadd constraintpublications_user_id_for eignforeign key (user_id) referencesregistrations())
Exception trace:
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
PDO::prepare("alter table
publicationsadd constraintpublications_user _id_foreignforeign key (user_id) referencesregistrations()") 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.
$table->foreign('user_id')->references('id')->on('registrations');yourreferencesword is incorrect