gpt4 book ai didi

php - 照亮\数据库\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064

转载 作者:行者123 更新时间:2023-11-29 07:28:24 29 4
gpt4 key购买 nike

我正在尝试将在 laravel 中创建的表迁移到数据库并使用外键。但是我收到以下错误。请帮我看看哪里出错了?

Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 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 ())

异常跟踪:

  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

  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

请使用参数 -v 以查看更多详细信息。

父表为registration,其代码如下。

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

第三个表我也使用了外键,作为注册表的主键是 publications,它的代码如下。

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

错误在上面,我已经粘贴了。但是 publication 表正在迁移到数据库,而其他两个表 registrations 和 educations 没有迁移。

最佳答案

我看到你在你的迁移中拼错了单词 references ,你能在你所有的迁移中更新它吗:

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

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

关于php - 照亮\数据库\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52722661/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com