我正在尝试将在laravel中创建的表迁移到数据库并使用外键。 Buut我遇到以下错误。请帮助我,错误在哪里?
Illuminate \ Database \ QueryException:SQLSTATE [42000]:语法错误或访问 违反:1064您的SQL语法有错误;检查手册 响应您的MariaDB服务器版本,以在的')'附近使用正确的语法 第1行(SQL:alter table
publications
添加约束publications_user_id_for eign
外键(user_id
)引用registrations
())
异常跟踪:
PDOException::(“ SQLSTATE [42000]:语法错误或访问冲突:1064 Yo 您的SQL语法有误;检查与您的马对应的手册 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');
}
}
我使用外键的第二个表的代码是教育,其代码如下。
<?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');
}
}
该错误已经在上面粘贴了。但是发布表正在迁移到数据库,而其他两个表的注册和教育没有迁移。
答案 0 :(得分:1)
我看到您在迁移中拼错了references
一词,是否可以在所有迁移中将其更新为:
...->references('...')...
如果这不能解决问题,则您的问题按迁移顺序执行,这意味着在运行迁移时文件顺序很重要