在特定的其他列之前或之后添加 sql 表列 - 通过 Laravel 4.1 中的迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20982538/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:42:06  来源:igfitidea点击:

Add sql table column before or after specific other column - by migrations in Laravel 4.1

laravellaravel-4database-migration

提问by Lajdák Marek

Table 'users':

表“用户”:

|id|name|address|post_code|deleted_at|created_at|

and I want add column 'phone_nr' somewhere between 'id' and 'deleted_at'

我想在“id”和“deleted_at”之间的某处添加“phone_nr”列

Is it possible by migrations in Laravel 4.1?

是否可以通过 Laravel 4.1 中的迁移来实现?

回答by James Binford

Yes. Create a new migration using php artisan migrate:make update_users_table.

是的。使用php artisan migrate:make update_users_table.

Then use the tablecommand as follows (if you're using MySQL!):

然后使用table如下命令(如果你使用的是 MySQL!):

Schema::table('users', function($table)
{
    $table->string('phone_nr')->after('id');
});

Once you've finished your migration, save it and run it using php artisan migrateand your table will be updated.

完成迁移后,保存并使用它运行它php artisan migrate,您的表将被更新。

Documentation: https://laravel.com/docs/4.2/migrations#column-modifiers

文档:https: //laravel.com/docs/4.2/migrations#column-modifiers

回答by Nick

The answer by James is still correct. However, Laravel 5 has slightly changed the make migration syntax:

詹姆斯的回答仍然是正确的。但是,Laravel 5 稍微改变了 make 迁移语法:

php artisan make:migration add_google_auth_to_users_table --table=users

(I am aware this question is tagged Laravel 4, however it ranks quite high for the question ;) )

(我知道这个问题被标记为 Laravel 4,但它在这个问题上的排名相当高;))

回答by Adambean

For anyone else wondering about @rahulsingh's query about adding multiple columns back to back after a single specific column:

对于想知道@rahulsingh 关于在单个特定列后背靠背添加多个列的查询的其他人:

$table->integer('col1')->after('ref_col');
$table->integer('col2')->after('what');

You can do this just by writing the new fields in reverse order, for example:

您只需按相反顺序编写新字段即可完成此操作,例如:

$table->integer('col4')->after('ref_col');
$table->integer('col3')->after('ref_col');
$table->integer('col2')->after('ref_col');
$table->integer('col1')->after('ref_col');

When you run this migration you'll find that your new fields col, col2, ... are in your intended order after the reference field ref_col.

当您运行此迁移时,您会发现您的新字段col, col2, ... 在参考字段之后按照您的预期顺序排列ref_col

Unfortunately there is no before()function for adding fields before an existing field. (If that existed we could keep the above statements in their real order.)

不幸的是,没有before()在现有字段之前添加字段的功能。(如果存在,我们可以按实际顺序保留上述语句。)



You cannotdo chaining with fields that do not yet exist, for example:

不能对尚不存在的字段进行链接,例如:

$table->integer('col1')->after('ref_col');
$table->integer('col2')->after('col1');
$table->integer('col3')->after('col2');
$table->integer('col4')->after('col3');

This is because the migration blueprint is compiled to a single query thus executed as one, so when it comes to adding col2your database engine will complain that col1doesn't exist.

这是因为迁移蓝图被编译为单个查询,因此作为一个查询执行,因此在添加col2数据库引擎时会抱怨col1不存在。

回答by Mexidense

I've created following migration:

我创建了以下迁移:

php artisan make:migration update_nvm_table

php artisan make:migration update_nvm_table

2019_07_10_130441_update_nvm_table.php

And inside:

里面:

public function up()
    {
        Schema::table('nvm', function (Blueprint $table) {
            $table->renameColumn('data1', 'call_owner');
            $table->renameColumn('data2', 'transfer_type');
            $table->string('transfer_data', 50)->nullable();
        });
    }

 public function down()
    {
        Schema::table('nvm', function (Blueprint $table) {
            $table->dropColumn('transfer_data');
            $table->renameColumn('call_owner', 'data1');
            $table->renameColumn('transfer_type', 'data2');
        });
    }

I hope it can a reference for you! :D

希望能给大家参考!:D

回答by Shaan

For latest versions of Laravel say 5-6

对于最新版本的 Laravel,请说 5-6

The command is

命令是

php artisan make:migration update_users_table

Then

然后

php artisan migrate