我有两张表(产品、品牌),其结构如下:
#products
id<pk,int> | brand_id<fk, int> | title<string>
#brands
id<pk,int> | title<string>
现在我的挑战是我必须通过迁移将主键从 int 更改为 UUID。由于#products 中的brand_id 是外键,因此该键还有一个索引,必须删除/重新创建该索引,以避免由于类型不兼容而导致错误。
这是我的迁移文件目前的样子:
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
Schema::withoutForeignKeyConstraints(function () use ($table) {
$table->dropForeignIdFor(Brand::class);
$table->char('brand_id', 36)->change();
});
});
}
但是,我仍然遇到以下异常,我不知道为什么:
General error: 3780 Referencing column 'brand_id' and referenced column 'id' in foreign key constraint 'products_brand_id_foreign' are incompatible.
你知道我做错了什么吗?
看来您删除约束的方法不起作用。
尝试使用其他方法删除:
$table->dropForeign(['brand_id']);