Laravel 迁移更改以使列可空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24419999/
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 Migration Change to Make a Column Nullable
提问by user391986
I created a migration with unsigned user_id
. How can I edit user_id
in a new migration to also make it nullable()
?
我用 unsigned 创建了一个迁移user_id
。如何user_id
在新迁移中进行编辑以使其也成功nullable()
?
Schema::create('throttle', function(Blueprint $table)
{
$table->increments('id');
// this needs to also be nullable, how should the next migration be?
$table->integer('user_id')->unsigned();
}
回答by MURATSPLAT
Laravel 5 now supports changing a column; here's an example from the offical documentation:
Laravel 5 现在支持更改列;这是官方文档中的一个示例:
Schema::table('users', function($table)
{
$table->string('name', 50)->nullable()->change();
});
Source: http://laravel.com/docs/5.0/schema#changing-columns
来源:http: //laravel.com/docs/5.0/schema#changed-columns
Laravel 4 does not support modifying columns, so you'll need use another technique such as writing a raw SQL command. For example:
Laravel 4 不支持修改列,因此您需要使用另一种技术,例如编写原始 SQL 命令。例如:
// getting Laravel App Instance
$app = app();
// getting laravel main version
$laravelVer = explode('.',$app::VERSION);
switch ($laravelVer[0]) {
// Laravel 4
case('4'):
DB::statement('ALTER TABLE `pro_categories_langs` MODIFY `name` VARCHAR(100) NULL;');
break;
// Laravel 5, or Laravel 6
default:
Schema::table('pro_categories_langs', function(Blueprint $t) {
$t->string('name', 100)->nullable()->change();
});
}
回答by Dmitri Chebotarev
Here's the complete answer for the future reader. Note that this is only possible in Laravel 5+.
这是未来读者的完整答案。请注意,这仅适用于 Laravel 5+。
First of all you'll need the doctrine/dbalpackage:
首先,您需要dotric/dbal包:
composer require doctrine/dbal
Now in your migration you can do this to make the column nullable:
现在在您的迁移中,您可以执行此操作以使列可以为空:
public function up()
{
Schema::table('users', function (Blueprint $table) {
// change() tells the Schema builder that we are altering a table
$table->integer('user_id')->unsigned()->nullable()->change();
});
}
You may be wondering how to revert this operation. Sadly this syntax is not supported:
您可能想知道如何还原此操作。遗憾的是,不支持此语法:
// Sadly does not work :'(
$table->integer('user_id')->unsigned()->change();
This is the correct syntax to revert the migration:
这是恢复迁移的正确语法:
$table->integer('user_id')->unsigned()->nullable(false)->change();
Or, if you prefer, you can write a raw query:
或者,如果您愿意,可以编写一个原始查询:
public function down()
{
/* Make user_id un-nullable */
DB::statement('UPDATE `users` SET `user_id` = 0 WHERE `user_id` IS NULL;');
DB::statement('ALTER TABLE `users` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;');
}
Hopefully you'll find this answer useful. :)
希望你会发现这个答案很有用。:)
回答by Unnawut
I assume that you're trying to edit a column that you have already added data on, so dropping column and adding again as a nullable column is not possible without losing data. We'll alter
the existing column.
我假设您正在尝试编辑已添加数据的列,因此在不丢失数据的情况下删除列并再次添加为可空列是不可能的。我们将alter
现有的列。
However, Laravel's schema builder does not support modifying columns other than renaming the column. So you will need to run raw queries to do them, like this:
但是,Laravel 的模式构建器除了重命名列之外,不支持修改列。因此,您需要运行原始查询来执行它们,如下所示:
function up()
{
DB::statement('ALTER TABLE `throttle` MODIFY `user_id` INTEGER UNSIGNED NULL;');
}
And to make sure you can still rollback your migration, we'll do the down()
as well.
为了确保您仍然可以回滚您的迁移,我们也会这样做down()
。
function down()
{
DB::statement('ALTER TABLE `throttle` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;');
}
One note is that since you are converting between nullable and not nullable, you'll need to make sure you clean up data before/after your migration. So do that in your migration script both ways:
一个注意事项是,由于您要在可为空和不可为空之间进行转换,因此您需要确保在迁移之前/之后清理数据。因此,在您的迁移脚本中以两种方式执行此操作:
function up()
{
DB::statement('ALTER TABLE `throttle` MODIFY `user_id` INTEGER UNSIGNED NULL;');
DB::statement('UPDATE `throttle` SET `user_id` = NULL WHERE `user_id` = 0;');
}
function down()
{
DB::statement('UPDATE `throttle` SET `user_id` = 0 WHERE `user_id` IS NULL;');
DB::statement('ALTER TABLE `throttle` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;');
}
回答by Yauheni Prakopchyk
He're the full migration for Laravel 5:
他是Laravel 5的完整迁移:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->unsignedInteger('user_id')->nullable()->change();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->unsignedInteger('user_id')->nullable(false)->change();
});
}
The point is, you can remove nullable
by passing false
as an argument.
关键是,您可以nullable
通过false
作为参数传递来删除。
回答by ken
If you happens to change the columns and stumbled on
如果您碰巧更改了列并偶然发现
'Doctrine\DBAL\Driver\PDOMySql\Driver' not found
then just install
然后安装
composer require doctrine/dbal
composer require doctrine/dbal
回答by rzb
Adding to Dmitri Chebotarev's answer, as for Laravel 5+.
添加到 Dmitri Chebotarev 的答案,至于 Laravel 5+。
After requiring the doctrine/dbalpackage:
在需要学说/dbal包之后:
composer require doctrine/dbal
You can then make a migration with nullable columns, like so:
然后,您可以使用可为空的列进行迁移,如下所示:
public function up()
{
Schema::table('users', function (Blueprint $table) {
// change() tells the Schema builder that we are altering a table
$table->integer('user_id')->unsigned()->nullable()->change();
});
}
To revert the operation, do:
要恢复操作,请执行以下操作:
public function down()
{
/* turn off foreign key checks for a moment */
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
/* set null values to 0 first */
DB::statement('UPDATE `users` SET `user_id` = 0 WHERE `user_id` IS NULL;');
/* alter table */
DB::statement('ALTER TABLE `users` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;');
/* finally turn foreign key checks back on */
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
回答by Sameer
Adding to Dmitri Chebotarev Answer,
添加到 Dmitri Chebotarev 答案,
If you want to alter multiple columns at a time , you can do it like below
如果你想一次改变多列,你可以像下面那样做
DB::statement('
ALTER TABLE `events`
MODIFY `event_date` DATE NOT NULL,
MODIFY `event_start_time` TIME NOT NULL,
MODIFY `event_end_time` TIME NOT NULL;
');
回答by Adil
Try it:
尝试一下:
$table->integer('user_id')->unsigned()->nullable();
回答by Debiprasad
For Laravel 4.2, Unnawut's answer above is the best one. But if you are using table prefix, then you need to alter your code a little.
对于 Laravel 4.2,上面 Unnawut 的回答是最好的。但是如果你使用的是表前缀,那么你需要稍微改变你的代码。
function up()
{
$table_prefix = DB::getTablePrefix();
DB::statement('ALTER TABLE `' . $table_prefix . 'throttle` MODIFY `user_id` INTEGER UNSIGNED NULL;');
}
And to make sure you can still rollback your migration, we'll do the down()
as well.
为了确保您仍然可以回滚您的迁移,我们也会这样做down()
。
function down()
{
$table_prefix = DB::getTablePrefix();
DB::statement('ALTER TABLE `' . $table_prefix . 'throttle` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;');
}