gpt4 book ai didi

php - Voyager 安装 Laravel 上的外键约束格式不正确

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

我正在尝试在我的 laravel 应用程序上安装 voyager,当我在控制台中运行“php artisan voyager:install --with-dummy”时,我收到此错误

PS C:\xampp\htdocs\newlootodds> php artisan voyager:install --with-dummy
Publishing the Voyager assets, database, and config files
Copied Directory [\vendor\tcg\voyager\publishable\database\seeds] To [\database\seeds]
Publishing complete.
Publishing complete.
Migrating the database tables into your application
Migrating: 2016_11_30_141208_create_permission_role_table

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `laravelapp`.`permissio
n_role` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `permission_role` add constraint `
permission_role_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`) on delete cascade)

at C:\xampp\htdocs\newlootodds\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| }
668|

Exception trace:

1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `laravelapp`.`permiss
ion_role` (errno: 150 "Foreign key constraint is incorrectly formed")")
C:\xampp\htdocs\newlootodds\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:119

2 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `laravelapp`.`permission_role` (errno: 150
"Foreign key constraint is incorrectly formed")")
C:\xampp\htdocs\newlootodds\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:117

我花了两天时间尝试在谷歌上寻找解决方案,但仍然没有找到任何东西..

最佳答案

检查您的“2016_11_30_141208_create_permission_role_table”迁移。据我了解,您有一个数据透视表。我认为你有一些问题,比如这样

public function up()
{
Schema::create('permission_role', function(Blueprint $table)
{
$table->bigIncrements('id');

$table->bigInteger('permission_id')->unsigned();
$table->bigInteger('role_id')->unsigned();

$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
}

由于 bigIncrements() 创建无符号整数列,因此您也需要将外键列定义为无符号整数。因此,请确保您更改为:

public function up()
{
Schema::create('permission_role', function(Blueprint $table)
{
$table->bigIncrements('id');

$table->unsignedBigInteger('permission_id')->unsigned();
$table->unsignedBigInteger('role_id')->unsigned();

$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
}

顺便说一句,如果没有帮助,那么我至少需要查看您的 3 个迁移(权限、角色、permission_role)

关于php - Voyager 安装 Laravel 上的外键约束格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58203619/

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