简体   繁体   English

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

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

I am trying to migrate tables created in laravel to database and using foreign key.我正在尝试将在 laravel 中创建的表迁移到数据库并使用外键。 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; Illuminate\\Database\\QueryException : SQLSTATE[42000]: 语法错误或访问冲突:1064 你的 SQL 语法有错误; check the manual that cor responds to your MariaDB server version for the right syntax to use near ')' at line 1 (SQL: alter table publications add constraint publications_user_id_for eign foreign key ( user_id ) references registrations ())检查手册是COR响应您的MariaDB的服务器版本使用在1号线附近的“)”正确的语法(SQL:ALTER TABLE publications添加约束publications_user_id_for eign外键( user_id )引用registrations ())

Exception trace:异常跟踪:

  1. 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 PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1064 你的 SQL 语法有错误;检查与你的 MariaDB 服务器版本相对应的手册,以获取在 ')' 附近使用的正确语法1") C:\\xampp\\htdocs\\plearning\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:452

  2. PDO::prepare("alter table publications add constraint publications_user _id_foreign foreign key ( user_id ) references registrations ()") 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

Please use the argument -v to see more details.请使用参数 -v 查看更多详细信息。

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.但是发布表正在迁移到数据库,而其他两个表注册和教育没有迁移。

I see you have misspelled the word references inside your migrations, could you update it to in all your migrations:我看到您在迁移中拼错了references一词,您能否在所有迁移中将其更新为:

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

If this doesn't fix the issue, then your issue is in the order of execution of migrations, meaning that your file order matters when migrations are ran如果这不能解决问题,那么您的问题在于迁移的执行顺序,这意味着在运行迁移时您的文件顺序很重要

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

相关问题 Illuminate\Database\QueryException: SQLSTATE[42000]: 语法错误或访问冲突:1064 - Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1064 SQLSTATE [42000]:语法错误或访问冲突:1064? - SQLSTATE[42000]: Syntax error or access violation: 1064? SQLSTATE [42000]:语法错误或访问冲突:1064 - SQLSTATE[42000]: Syntax error or access violation: 1064 SQLSTATE [42000]:语法错误或访问冲突:1064 - SQLSTATE[42000]: Syntax error or access violation: 1064 PDO错误:SQLSTATE [42000]:语法错误或访问冲突:1064 - PDO ERROR:SQLSTATE[42000]: Syntax error or access violation: 1064 错误:SQLSTATE [42000]:语法错误或访问冲突:1064-PHP MYSQL - Error: SQLSTATE[42000]: Syntax error or access violation: 1064 - PHP MYSQL 错误Laravel框架:SQLSTATE [42000]:语法错误或访问冲突:1064 - Error Laravel Framework: SQLSTATE[42000]: Syntax error or access violation: 1064 如何纠正“ SQLSTATE [42000]:语法错误或访问冲突:1064”错误 - how to rectify “SQLSTATE[42000]: Syntax error or access violation: 1064 ” error CakePHP 3错误:SQLSTATE [42000]:语法错误或访问冲突:1064 - CakePHP 3 Error: SQLSTATE[42000]: Syntax error or access violation: 1064 mysql 数据库 SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有误 - mysql database SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM