Laravel 迁移将列类型从 varchar 更改为 longText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37724150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel migrations change a column type from varchar to longText
提问by Marco
I need to change with migration column type of $table->string('text');
to a text type, I have tried to do that in few ways, but none of them worked. Is it possible to do it in one migration. I could I guess drop the column and then create it again with new type, but I wonder if it is possible to do it in one migration?
我需要将迁移列类型更改$table->string('text');
为文本类型,我尝试通过几种方式来做到这一点,但都没有奏效。是否可以在一次迁移中完成。我可以猜测删除该列,然后使用新类型再次创建它,但我想知道是否可以在一次迁移中完成它?
回答by Alexey Mezenin
You can create a new migration and change just one column type:
您可以创建新迁移并仅更改一种列类型:
public function up()
{
Schema::table('sometable', function (Blueprint $table) {
$table->text('text')->change();
});
}
You need to install doctrine/dbal
to make this work
您需要安装doctrine/dbal
才能使这项工作
composer require doctrine/dbal
Works with Laravel 5.0+. It does not work with Laravel 4.2.
适用于 Laravel 5.0+。它不适用于 Laravel 4.2。
回答by zorx
According to Laravel Doc
You can do it like
你可以这样做
Schema::table('yourTable', function (Blueprint $table) {
$table->text('text')->change();
});
be sure to add the doctrine/dbal dependency to your composer.json file
一定要在你的 composer.json 文件中添加学说/dbal 依赖项
回答by Justin Origin Broadband
It's possible to do with a TABLE migration.
可以使用 TABLE 迁移。
As mentioned in other posts, be sure to run composer install doctrine/dbal
from your project root.
正如其他帖子中提到的,一定composer install doctrine/dbal
要从你的项目根目录运行。
These are set up with:
这些设置为:
php artisan make:migration alter_table_[yourtablenamehere]_change_[somecolumnname] --table=[yourtablenamehere]
from your project root.
从你的项目根。
From the Documentation:
从文档:
https://laravel.com/docs/master/migrations#modifying-columns
https://laravel.com/docs/master/migrations#modifying-columns
class AlterTableSomething extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('table', function (Blueprint $table) {
$table->text('column_name')->change();
});
}
}
回答by Kamil Kie?czewski
If you get following error using change()
如果您使用以下错误 change()
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL80Platform may not support it.
请求的数据库类型未知,Doctrine\DBAL\Platforms\MySQL80Platform 可能不支持。
this means that exists some column (not necessarily changed one) in your table which has enum type. So instead using change()
you can use following function:
这意味着在您的表中存在一些具有枚举类型的列(不一定是更改的列)。因此,change()
您可以使用以下功能来代替使用:
public function changeColumnType($table, $column, $newColumnType) {
DB::statement("ALTER TABLE $table CHANGE $column $column $newColumnType");
}
And use it like that: $this->changeColumnType('sometable','text','TEXT');
并像这样使用它: $this->changeColumnType('sometable','text','TEXT');