繁体   English   中英

Illuminate\\Database\\QueryException:SQLSTATE[42000]:语法错误或访问冲突:1064

[英]Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064

我正在尝试将在 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 你的 SQL 语法有错误;检查与你的 MariaDB 服务器版本相对应的手册,以获取在 ')' 附近使用的正确语法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');
    }
}

我使用外键的第二个表的代码是教育,其代码如下。

    <?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');
    }
}

错误在我已经粘贴的上面。 但是发布表正在迁移到数据库,而其他两个表注册和教育没有迁移。

我看到您在迁移中拼错了references一词,您能否在所有迁移中将其更新为:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM