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

标签 php mysql laravel laravel-6 laravel-migrations

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/

相关文章:

php - 我可以创建一个 PHP 函数来创建文本输入字段吗?

java - Tomcat Web 应用程序中的单例和类加载器

php - Laravel 容器和绑定(bind)

php - 获取/设置 magento 表中的自定义列

PHP : Function argument must be an Object with dynamic class name

当行数多于 IN 值时,MYSQL 返回 null

php - 带有由 php 调用的变量的 SQL 查询问题

php - 使用路由时找不到基表或 View

laravel - Laravel 4 中检索特定日期范围内数据记录的查询

php 脚本正在输出 php 代码,不知道为什么