sql - SQLSTATE [HY000]:一般错误:外键定义中有 1 个未知列“user_id”
问题描述
运行时出现此错误:
php artisan migrate:fresh
Illuminate\Database\QueryException:SQLSTATE[HY000]:一般错误:外键定义中有 1 个未知列“user_id”(SQL:创建表“users”(“id”整数不为空主键自动增量,“name”varchar 不为空, “email” varchar 不为 null,“username” varchar 不为 null,“email_verified_at” datetime null,“password” varchar 不为 null,“remember_token” varchar null,“created_at” datetime null,“updated_at” datetime null,外键(“user_id ") 在删除级联时引用 "users"("id")))
我正在关注 youtube 上的视频教程,教程的代码是这样的:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('profiles');
}
}
如果我复制并粘贴此代码,则会出现错误。所以我在stackoverflow上搜索,我找到了这个解决方案:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->index('user_id');
});
Schema::table('profiles', function (Blueprint $table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('profiles');
}
这是我的用户表:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('users');
}
但是今天,当我跑步时,php artisan migrate:fresh
我又遇到了这个错误。
我该如何解决?
谢谢
解决方案
正如其他人所提到的,user_id
不是表中的列users
,但您正在尝试在其上创建索引。这一行:
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
属于profiles
表创建模式,而不是users
表创建模式。
完整代码:
// create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
// create_profiles_table.php <-- migrate AFTER users table
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
推荐阅读
- ruby-on-rails - ActiveRecord::Base.connected?即使 DB 关闭也返回 true
- php - 将实体传递给 Form 时的 TransformationFailedException
- windows-installer - MSP 安装期间出现错误 1603
- arrays - 自适应排序算法与排序网络对 32 个随机元素的列表进行排序
- reactjs - cxjs 如何在选择中获取数组或对象的所有键?
- python - 如何优雅地迭代列表或字典
- talend - 从 HP ALM 提取数据时是否可以设置限制
- jenkins - Jenkins 管道 - mvn 命令无法正确读取 withCredentials 变量
- scala - spark-submit的输出结果
- sql-server - 在 SSIS 中导入 CSV:截断错误