【发布时间】:2019-03-14 07:52:59
【问题描述】:
我正在尝试将在 laravel 中创建的表迁移到数据库并使用外键。但我收到以下错误。请帮我看看哪里出错了?
Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或访问 违规:1064 您的 SQL 语法有错误;检查手册,cor 响应您的 MariaDB 服务器版本,以便在 ')' 附近使用正确的语法 第1行(SQL:alter table
publications添加约束publications_user_id_for eign外键(user_id)引用registrations())
异常跟踪:
PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1064 Yo 你的 SQL 语法有错误;检查与您的 Ma 相对应的手册 riaDB 服务器版本,用于在第 1 行的 ')' 附近使用正确的语法") C:\xampp\htdocs\plearning\vendor\laravel\framework\src\Illuminate\Database \Connection.php:452
PDO::prepare("修改表
publications添加约束publications_user _id_foreign外键(user_id)引用registrations()") C:\xampp\htdocs\plearning\vendor\laravel\framework\src\Illuminate\Database \Connection.php:452
请使用参数 -v 查看更多详细信息。
父表是注册表,代码如下。
<?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');
}
}
我使用外键的第二个表的代码是educations,其代码如下。
<?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');
}
}
我使用的第三个表也是外键,作为注册表的主键是出版物,其代码如下。
<?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');
}
}
错误在我已经粘贴的上面。但是发布表正在迁移到数据库,而其他两个表注册和教育没有迁移。
【问题讨论】:
-
$table->foreign('user_id')->references('id')->on('registrations');你的references字不正确