【问题标题】:Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064Illuminate\Database\QueryException:SQLSTATE[42000]:语法错误或访问冲突:1064
【发布时间】: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())

异常跟踪:

  1. PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1064 Yo 你的 SQL 语法有错误;检查与您的 Ma 相对应的手册 riaDB 服务器版本,用于在第 1 行的 ')' 附近使用正确的语法") C:\xampp\htdocs\plearning\vendor\laravel\framework\src\Illuminate\Database \Connection.php:452

  2. 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-&gt;foreign('user_id')-&gt;references('id')-&gt;on('registrations'); 你的references 字不正确

标签: php mysql laravel


【解决方案1】:

我发现您在迁移中拼错了单词 references,您能否在所有迁移中将其更新为:

...-&gt;references('...')...

如果这不能解决问题,那么您的问题在于迁移的执行顺序,这意味着在运行迁移时您的文件顺序很重要

【讨论】:

  • 同样的问题,发布表迁移了,其他两个没有迁移
  • 你能检查迁移文件的顺序吗,你的顺序应该是从上到下:registrations, educations, publications
  • 我怎样才能改变它
  • 要更改文件的顺序,你需要重命名文件,只需重命名文件名中的dates,直到你得到正确的顺序
猜你喜欢
  • 2023-04-10
  • 2018-12-24
  • 2017-10-08
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2017-10-29
相关资源
最近更新 更多