运行'php artisan migrate'时"IlluminateDatabaseQueryException"拉拉维尔错误



无法运行"php artisan migrate"在laravel项目中。如果我运行这个命令,那么下面的错误将显示。

迁移:2021 _08_02_173519_create_access_user

照亮数据库 QueryException

SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'access_id' does not exist in table (SQL: alter tableaccess_useradd constraintaccess_user_access_id_foreignforeign Key (access_id) referencesaccess(id))

在C:Users HETTIARACHCHIGEDAMIT LanexGloble lanex-internal-backend api供应商数据库 laravel framework src 照亮 Connection.php: 692688//如果在尝试运行查询时发生异常,我们将格式化错误689//消息包含与SQL的绑定,这将使此异常成为一个异常690//对开发人员更有帮助,而不仅仅是数据库的错误。691 (Exception $e) {693 $query, $this->prepareBindings($bindings)694年▕ );695年▕ }696年▕}

1 C:UsersHETTIARACHCHIGEDAMITLanexGloblelanex-internal-backendapivendorlaravelframeworksrcIlluminateDatabaseConnection.php:485 DatabaseConnePDOException::("SQLSTATE[42000]:语法错误或访问冲突:1072键列'access_id'不存在于表") in表")

2 C:Users HETTIARACHCHIGEDAMIT LanexGloble lanex-internal-backend api供应商 laravel framework src 照亮数据库数据库 Conne Connection.php: 485PDOStatement: execute ()

这是一个错误文件。

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateAccessUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('access_user', function (Blueprint $table) {
$table->increments('id');
$table->foreign('access_id')->references('id')->on('access');
$table->foreign('user_id')->references('id')->on('user');
$table->boolean('status');
$table->timestamp('updated_at');
$table->timestamp('created_at');
$table->string('updated_by');
$table->string('created_by');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('access_user');
}
} 

您应该在添加外键之前添加一个列access_idforeign()正在为添加外键列。

因此,您应该将迁移更改为以下内容:
public function up()
{
Schema::create('access_user', function (Blueprint $table) {
$table->increments('id');
// add this line
$table->unsignedBigInteger('access_id');  // depend on your foreign column type
$table->foreign('access_id')->references('id')->on('access');
$table->foreign('user_id')->references('id')->on('user');
$table->boolean('status');
$table->timestamp('updated_at');
$table->timestamp('created_at');
$table->string('updated_by');
$table->string('created_by');
});
}

或使用foreignId():

public function up()
{
Schema::create('access_user', function (Blueprint $table) {
$table->increments('id');
$table->foreignId('access_id')->constrained('access');  // change here
$table->foreign('user_id')->references('id')->on('user');
$table->boolean('status');
$table->timestamp('updated_at');
$table->timestamp('created_at');
$table->string('updated_by');
$table->string('created_by');
});
}

见文档。

相关内容

  • 没有找到相关文章

最新更新