即使不添加外键也无法添加外键约束
Cannot Add Foreign Key Constraint even no foreign keys are added
Laravel 8
出于某种原因,我的暂存环境不允许我 运行 迁移。
我收到以下错误。
General error: 1215 Cannot add foreign key constraint (
SQL: create table `login_credentials` (
`id` char(36) not null,
`username` varchar(75) not null,
`confirmation_code` varchar(36) null,
`deleted_at` timestamp null,
`remember_token` varchar(100) null,
`created_at` timestamp null,
`updated_at` timestamp null)
default character set utf8mb4
collate 'utf8mb4_unicode_ci'
engine = InnoDB)
我还没有定义任何外键,这似乎是最近才开始的。我为我的应用程序编写了单元测试,所以我的大部分开发都是在本地完成的。
我很少会在推送到生产环境之前上传到暂存服务器以进一步测试更详细的内容。如果它是一个小的可协商更改,那么一旦单元测试通过并验证干净的构建,它将从本地转到生产。不幸的是,从现在到几周前,这成为了一个问题。
我的迁移文件 login_credentials。
class CreateLoginCredentialsTable extends Migration
{
public function up(): void
{
Schema::create('login_credentials', function (Blueprint $table) {
$table->uuid('id')->nullable(false)->unique()->primary();
$table->string('username',75)->unique();
$table->string('confirmation_code',36)->nullable()->unique();
$table->softDeletes();
$table->rememberToken();
$table->timestamps();
});
}
}
我在 Whosebug 上进行了高低搜索,并尝试了以下方法。
- 验证我的用户
id
的列作为 uuid 在任何地方都匹配。
- 验证数据库是 InnoDB
- 验证在任何迁移中都没有定义外键
- 验证迁移文件的执行顺序是否正确
- 已将作曲家更新为最新 dependencies/versions。
- 运行 用于清除缓存、会话、路由、视图等的所有 artisan 命令
- 当 tbl 的 id 是一个 uuid 时验证我的模型是 $incrementing = false。
- 将 laravel 迁移文件移动到我的迁移文件夹中,并将
Cashier::ignoreMigrations(); Passport::ignoreMigrations();
添加到我的 appserviceprovider 并更新这些迁移文件以确保 user_id 不是 bigInt 或 Int,并定义为uuid.
- 写一个 trait 来确保 laravel 知道 id 是一个 uuid。
运行在我的本地主机中迁移文件运行没问题,没有错误。
有什么想法吗?
即使删除了除单个 login_credentials table 之外的所有迁移文件,仍然出现相同的错误?我要疯了吗??
我的暂存数据库和生产数据库在同一台 mysql 服务器上。
来了解一下,您可能有一个来自具有相似或相同 table(s) 的不同数据库的外键约束,并在您正在使用的数据库上造成此类问题上。
回答而不是删除它,以防其他人偶然发现它作为面包屑。
“无法添加外键约束”
检查其他数据库的约束
Laravel 8
出于某种原因,我的暂存环境不允许我 运行 迁移。
我收到以下错误。
General error: 1215 Cannot add foreign key constraint (
SQL: create table `login_credentials` (
`id` char(36) not null,
`username` varchar(75) not null,
`confirmation_code` varchar(36) null,
`deleted_at` timestamp null,
`remember_token` varchar(100) null,
`created_at` timestamp null,
`updated_at` timestamp null)
default character set utf8mb4
collate 'utf8mb4_unicode_ci'
engine = InnoDB)
我还没有定义任何外键,这似乎是最近才开始的。我为我的应用程序编写了单元测试,所以我的大部分开发都是在本地完成的。
我很少会在推送到生产环境之前上传到暂存服务器以进一步测试更详细的内容。如果它是一个小的可协商更改,那么一旦单元测试通过并验证干净的构建,它将从本地转到生产。不幸的是,从现在到几周前,这成为了一个问题。
我的迁移文件 login_credentials。
class CreateLoginCredentialsTable extends Migration
{
public function up(): void
{
Schema::create('login_credentials', function (Blueprint $table) {
$table->uuid('id')->nullable(false)->unique()->primary();
$table->string('username',75)->unique();
$table->string('confirmation_code',36)->nullable()->unique();
$table->softDeletes();
$table->rememberToken();
$table->timestamps();
});
}
}
我在 Whosebug 上进行了高低搜索,并尝试了以下方法。
- 验证我的用户
id
的列作为 uuid 在任何地方都匹配。 - 验证数据库是 InnoDB
- 验证在任何迁移中都没有定义外键
- 验证迁移文件的执行顺序是否正确
- 已将作曲家更新为最新 dependencies/versions。
- 运行 用于清除缓存、会话、路由、视图等的所有 artisan 命令
- 当 tbl 的 id 是一个 uuid 时验证我的模型是 $incrementing = false。
- 将 laravel 迁移文件移动到我的迁移文件夹中,并将
Cashier::ignoreMigrations(); Passport::ignoreMigrations();
添加到我的 appserviceprovider 并更新这些迁移文件以确保 user_id 不是 bigInt 或 Int,并定义为uuid. - 写一个 trait 来确保 laravel 知道 id 是一个 uuid。
运行在我的本地主机中迁移文件运行没问题,没有错误。
有什么想法吗?
即使删除了除单个 login_credentials table 之外的所有迁移文件,仍然出现相同的错误?我要疯了吗??
我的暂存数据库和生产数据库在同一台 mysql 服务器上。
来了解一下,您可能有一个来自具有相似或相同 table(s) 的不同数据库的外键约束,并在您正在使用的数据库上造成此类问题上。
回答而不是删除它,以防其他人偶然发现它作为面包屑。
“无法添加外键约束” 检查其他数据库的约束