gpt4 book ai didi

php - 如何在 laravel 5.8 中错误地设置外键约束

转载 作者:行者123 更新时间:2023-11-29 07:22:03 24 4
gpt4 key购买 nike

<分区>

我有一些表要迁移,后面有。如果我尝试将该表迁移到服务器数据库,它会显示一般错误:1005。我在底部给出错误和服务器详细信息。请帮助我顺利迁移。

1.)用户

Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});

2.)公司

Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->longText('description');
$table->unsignedInteger('user_id');
$table->timestamps();

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

3.项目

Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->longText('description');
$table->unsignedInteger('company_id');
$table->unsignedInteger('user_id');
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('company_id')->references('id')->on('companies');
});

服务器详细信息

server: 127.0.0.1 via TCP/IP
Server type: MariaDB
Server connection: SSL is not being used Documentation
Server version: 10.1.38-MariaDB - mariadb.org binary distribution
Protocol version: 10
User: root@localhost
Server charset: UTF-8 Unicode (utf8)
DB Name: laravel

错误:

 Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `laravel`.`#sql-91c_30` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `companies` add constraint `companies_user_id_foreign` foreign key (`use
r_id`) references `users` (`id`))

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