use*_*986 172 nullable laravel eloquent laravel-5 laravel-migrations
我用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();
}
Run Code Online (Sandbox Code Playgroud)
MUR*_*LAT 225
Laravel 5现在支持更换列..
官方文档的例子
Schema::table('users', function($table)
{
$table->string('name', 50)->nullable()->change();
});
Run Code Online (Sandbox Code Playgroud)
来源:http://laravel.com/docs/5.0/schema#changing-columns
Laravel 4不支持修改列.你必须编写raw 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();
});
}
Run Code Online (Sandbox Code Playgroud)
小智 152
以下是未来读者的完整答案.请注意,这仅适用于Laravel 5+.
首先,你需要学说/ dbal包:
composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)
现在,在迁移中,您可以执行此操作以使列可为空:
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();
});
}
Run Code Online (Sandbox Code Playgroud)
您可能想知道如何还原此操作.遗憾的是,不支持此语法:
// Sadly does not work :'(
$table->integer('user_id')->unsigned()->change();
Run Code Online (Sandbox Code Playgroud)
这是还原迁移的正确语法:
$table->integer('user_id')->unsigned()->nullable(false)->change();
Run Code Online (Sandbox Code Playgroud)
或者,如果您愿意,可以编写原始查询:
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;');
}
Run Code Online (Sandbox Code Playgroud)
希望你会发现这个答案很有用.:)
Unn*_*wut 150
我假设您正在尝试编辑已添加数据的列,因此在不丢失数据的情况下删除列并再次添加为可为空的列是不可能的.我们将alter
现有专栏.
但是,Laravel的架构构建器不支持修改列,而不是重命名列.因此,您需要运行原始查询来执行它们,如下所示:
function up()
{
DB::statement('ALTER TABLE `throttle` MODIFY `user_id` INTEGER UNSIGNED NULL;');
}
Run Code Online (Sandbox Code Playgroud)
为了确保您仍然可以回滚迁移,我们也会这样做down()
.
function down()
{
DB::statement('ALTER TABLE `throttle` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;');
}
Run Code Online (Sandbox Code Playgroud)
需要注意的是,由于您要在可空和可空之间进行转换,因此您需要确保在迁移之前/之后清理数据.所以在迁移脚本中以这两种方式执行此操作:
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;');
}
Run Code Online (Sandbox Code Playgroud)
Yau*_*hyk 40
他是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();
});
}
Run Code Online (Sandbox Code Playgroud)
关键是,您可以nullable
通过false
作为参数传递来删除.
ken*_*ken 15
如果您碰巧更改了列并偶然发现了
'Doctrine\DBAL\Driver\PDOMySql\Driver' not found
Run Code Online (Sandbox Code Playgroud)
然后只需安装
composer require doctrine/dbal
Abd*_*una 11
我不得不使用nullable(true)
Schema::table('users', function($table)
{
$table->string('name', 50)->nullable(true)->change();
});
Run Code Online (Sandbox Code Playgroud)
composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)
成功安装 Composer 包后,我们可以使用迁移命令更改数据类型并更改列名称。
句法:
php artisan make:migration alter_table_[table_name]_change_[column_name] --table=[table_name]
Run Code Online (Sandbox Code Playgroud)
例子:
php artisan make:migration alter_table_sessions_change_user_id --table=sessions
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AlterTableSessionsChangeUserId extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('sessions', function (Blueprint $table) {
$table->integer('user_id')->unsigned()->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('sessions', function (Blueprint $table) {
$table->dropColumn('user_id');
});
}
}
Run Code Online (Sandbox Code Playgroud)
并运行:php artisan migrate
或刷新表以更改列名称。不使用change
方法。
Schema::create('throttle', function(Blueprint $table)
{
$table->increments('id');
# old code
$table->integer('user_id')->unsigned();
# new code
$table->integer('user_id')->unsigned()->nullable();
}
Run Code Online (Sandbox Code Playgroud)
注意:以下命令用于清除表中的数据。
php artisan migrate:refresh --path=/database/migrations/2021_09_31_050851_create_throttle_table.php
Run Code Online (Sandbox Code Playgroud)
与Laravel 5+一样,增加了Dmitri Chebotarev的答案。
要求教义/ dbal软件包后:
composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用可为空的列进行迁移,如下所示:
composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)
要还原操作,请执行以下操作:
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();
});
}
Run Code Online (Sandbox Code Playgroud)
尝试一下:
$table->integer('user_id')->unsigned()->nullable();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
166510 次 |
最近记录: |