laravel - 将外键 unsignedBigInteger 字段更改为迁移 laravel 中的文本字段
问题描述
我有一个表foo
和一个像这样的迁移(仅显示 up 方法):
public function up()
{
Schema::table('foo', function (Blueprint $table) {
$table->unsignedBigInteger('boo_id');
});
Schema::table('foo', function (Blueprint $table) {
$table->foreign('boo_id')->references('id')->on('boos');
});
}
所以我有一个boo_id
外键boos.id
。现在我想创建一个迁移,将字段更改boo_id
为文本而不是外键。我怎样才能做到这一点?
解决方案
您首先需要删除外键和为外键创建的索引,然后更改列的数据类型。像这样的迁移会有所帮助:
public function up()
{
Schema::table('foo', function (Blueprint $table) {
$table->dropForeign('foo_boo_id_foreign');
$table->dropIndex('foo_boo_id_foreign');
});
Schema::table('foo', function (Blueprint $table) {
$table->text('boo_id')->change();
});
}
请注意,它们必须在两个单独的Schema::table
主体中,否则您将面临错误:Syntax error or access violation: 1170 BLOB/TEXT column 'boo_id' used in key specification without a key length (SQL: ALTER TABLE foo CHANGE boo_id boo_id TEXT DEFAULT NULL)
. 另请注意,传递给您的名称dropForeign
和dropIndex
函数可能会有所不同,您应该在数据库中检查以确保,因为该命名约定不是强制性的。
推荐阅读
- sftp - Bloomberg 请求生成器的 C# 自动化
- java - 如何推断 JSON 文件的模式?
- angular - Service Fabric 中的 Angular 6 + ASP .NET Core MVC
- amazon-web-services - 是时候将 100GB 上传到 Amazon Glacier
- asp.net - 预编译视图的首次访问速度慢
- hibernate - 通过 JPA 和休眠在 Spring Boot 中使用 mysql 连接和 gradle
- python - 在python中读取受密码保护的pdf?
- sql-server - MS SQL 插入触发器检查列中正确的数字格式
- php - 密码哈希和在 Codeigniter 中
- angular6 - 如何在角度 6 中设置应用程序变量?