【问题标题】:Laravel 8 Problem Foreign key in two table errorLaravel 8 问题外键在两个表错误
【发布时间】:2022-01-29 09:39:14
【问题描述】:

在创建用户表模式和 user_type 表模式后,它经常给我外键问题我将 db 设置为 utf8 如果它可能是一个问题,我已经打了几个小时但我不能摆脱它。

Schema::create('users_panel', function (Blueprint $table) {
        $table
            ->id();
        $table
            ->unsignedBigInteger('id_type_user');
        $table
            ->string('company_name');
        $table
            ->string('email')->unique();
        $table
            ->string('password', 60);
        $table
            ->timestamp('email_verified_at')
            ->nullable();
        $table
            ->string('representative_name')
            ->nullable();
        $table
            ->string('phone_number')
            ->nullable();
        $table
            ->string('address');
        $table
            ->rememberToken();
        $table
            ->timestamps();
    });

    Schema::table('users_panel', function (Blueprint $table) {
        $table
            ->foreign('id_type_user')
            ->references('id')
            ->on('type_users')
            ->onDelete('cascade');
    });

type_users:

Schema::create('type_users', function (Blueprint $table) {
        $table->id();
        $table->string('type_user');
        $table->timestamps();
    });

错误: PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表prysma_db.users_panel (errno: 150 "外键约束格式不正确")")

【问题讨论】:

    标签: mysql laravel-8 php-8


    【解决方案1】:

    你应该遵循这个: https://laravel.com/docs/8.x/migrations#renaming-tables-with-foreign-keys

    我认为你在这里混淆了其他东西。如果你能与 line 分享错误会更好。

    【讨论】:

    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多