gpt4 book ai didi

php - 照亮\数据库\QueryException SQLSTATE HY000 : General error: 1215

转载 作者:行者123 更新时间:2023-11-30 21:59:58 29 4
gpt4 key购买 nike

我目前正在做一个学校项目,我的数据库遇到了这个问题,我想创建与视频和类别的关系,这是我的两个表。视频表:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->default('');
$table->string('url')->default('');

$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories');

$table->string('img_url')->default('');
$table->integer('views')->nullable();
$table->integer('rating')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('videos');
}
}

和类别表:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}

当我在控制台中运行 php artisan migrate:refresh 时出现此错误:

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 
Cannot add foreign key constraint (SQL: alter table `videos` add constraint
`videos_category_id_foreign` foreign key (`category_id`) references
`categories` (`id`))

[PDO异常] SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束

我不明白为什么,因为当我改变这个时:

$table->foreign('category_id')->references('id')->on('categories');

为此:

$table->foreign('category_id')->references('id')->on('users');

一切正常,我没有任何错误,我希望有人能帮我解决这个问题!

最佳答案

你应该在视频表之前创建你的类别表

您可以使用它并删除您的类别迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// create categories table
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});

//now create videos table
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->default('');
$table->string('url')->default('');

$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories');

$table->string('img_url')->default('');
$table->integer('views')->nullable();
$table->integer('rating')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('videos');
Schema::dropIfExists('categories');
}
}

关于php - 照亮\数据库\QueryException SQLSTATE HY000 : General error: 1215,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43710363/

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