gpt4 book ai didi

php - Laravel 外键约束格式不正确 我已经搜索但找不到答案

转载 作者:行者123 更新时间:2023-11-29 09:26:02 26 4
gpt4 key购买 nike

class CreateMediaTable extends Migration
{
public function up()
{
Schema::create('media', function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('id_users');
$table->unsignedBigInteger('id_posts');
$table->char('type', 1); //P: photo or V: video
$table->string('file');
$table->timestamps();

$table->foreign('id_posts')->references('id')->on('posts');
$table->foreign('id_users')->references('id')->on('users');
});
}

public function down()
{
Schema::dropIfExists('posts');
}
}

和我的 createprofilemigration

/**
* @author Alex Madsen
*
* @date November 6, 2018
*/

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

class CreateUserProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_profiles', function (Blueprint $table) {
$table->unsignedInteger('id_users')->unique();
$table->string('handle')->unique();
$table->string('icon')->default('https://i.redd.it/130am13nj6201.png');
$table->string('profile_image')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
}

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

不断抛出这些错误,我错过了什么?请注意,我对此很陌生,尝试在休息时间通过 youtube 和 stackoverflow 学习。不确定该走哪条路。我查看了论坛并尝试了 $table->foreign('id_posts')->references('id')->on('posts');但这并没有解决问题。

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `ci`.`media` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter t
able `media` add constraint `media_id_posts_foreign` foreign key (`id_posts`) references `posts` (`id`))

at C:\xampp6\htdocs\lol\simple_social_network_laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('id_users');
$table->unsignedInteger('subreddit_id');
$table->text('description');
$table->string('title');
$table->text('content');
$table->unsignedInteger('id_posts');
$table->timestamps();

$table->foreign('id_users')->references('id')->on('users');
$table->foreign('subreddit_id')->references('id')->on('id_posts');
});
}

public function down()
{
Schema::dropIfExists('posts');
}
}

最佳答案

使用外键时类型应该完全相同。

您创建:

$table->unsignedBigInteger('id_posts');

所以它是无符号大整数,但可能在 posts 表中而不是 bigIncrements 中,您只使用 increments 作为 id > 列,这就是您收到此错误的原因。

所以很有可能代替:

$table->unsignedBigInteger('id_posts'); 

你应该使用

$table->unsignedInteger('id_posts'); 

或作为替代解决方案使用

$table->bigIncrements('id');

帖子迁移中

关于php - Laravel 外键约束格式不正确 我已经搜索但找不到答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59600306/

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