Ari*_*asa 62 php mysql database migration laravel
我有下面提到的专栏:
public function up()
{
Schema::create('stnk', function(Blueprint $table)
{
$table->increments('id');
$table->string('no_reg', 50)->unique();
$table->string('no_bpkb', 50)->unique();
$table->string('nama_pemilik', 100);
$table->string('alamat');
$table->string('merk', 50);
$table->string('tipe', 50);
$table->string('jenis', 50);
$table->smallInteger('tahun_pembuatan');
$table->smallInteger('tahun_registrasi');
$table->smallInteger('isi_silinder');
$table->string('no_rangka', 50);
$table->string('no_mesin', 50);
$table->string('warna', 50);
$table->string('bahan_bakar', 50);
$table->string('warna_tnkb', 50);
$table->string('kode_lokasi', 50);
$table->date('berlaku_sampai');
$table->timestamps();
$table->index('created_at');
$table->index('updated_at');
});
}
Run Code Online (Sandbox Code Playgroud)
我把播种机做成了桌子
现在我想重命名id
为id_stnk
.
我在"作曲家"中添加了一个"教义/ dbal "并做了一个composer update
.
我做了迁移php artisan migration:make rename_column
.
然后我为rename_column添加了新方法:
Schema::table('stnk', function(Blueprint $table)
{
$table->renameColumn('id', 'id_stnk');
});
Run Code Online (Sandbox Code Playgroud)
然后我试图运行命令php artisan migrate
但是我收到的错误如下所述:
[Ulluminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './my_database/#sql -447_33' to './my_database/stnk' (error: 150) (SQL: ALTER TABLE stnk CHANGE id id_stnk INT UNSIGENED AUTO_INCREMENT NOT NULL)
[PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './my_database/#sql -447_33' to './my_database/stnk' (error: 150)
Run Code Online (Sandbox Code Playgroud)
Lau*_*nce 89
您需要创建另一个迁移文件 - 并将其放在那里:
跑
Laravel 4: php artisan migrate:make rename_stnk_column
Laravel 5: php artisan make:migration rename_stnk_column
Run Code Online (Sandbox Code Playgroud)
然后在新的迁移文件中放置:
class RenameStnkColumn extends Migration
{
public function up()
{
Schema::table('stnk', function(Blueprint $table) {
$table->renameColumn('id', 'id_stnk');
});
}
public function down()
{
Schema::table('stnk', function(Blueprint $table) {
$table->renameColumn('id_stnk', 'id');
});
}
}
Run Code Online (Sandbox Code Playgroud)
小智 21
您要做的第一件事是创建迁移文件.
输入您的命令行
php artisan make:migration rename_stk_column --table="YOUR TABLE" --create
Run Code Online (Sandbox Code Playgroud)
创建文件后.在database/migrations下的app文件夹中打开新创建的迁移文件.
在你的up方法中插入以下内容:
Schema::table('stnk', function(Blueprint $table)
{
$table->renameColumn('id', 'id_stnk');
});
}
Run Code Online (Sandbox Code Playgroud)
在你的down方法中:
Schema::table('stnk', function(Blueprint $table)
{
$table->renameColumn('id_stnk', 'id);
});
}
Run Code Online (Sandbox Code Playgroud)
然后在你的命令行中输入
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
那么wollah!你刚刚将id重命名为id_stnk.BTW你可以使用
php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)
撤消更改.祝好运
bma*_*ovu 13
要重命名列,可以在"架构"构建器上使用renameColumn方法.*在重命名列之前,请务必将doctrine/dbal依赖项添加到composer.json文件中.*
或者您可以使用作曲家简单地要求包...
composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)
资料来源: https ://laravel.com/docs/5.0/schema#renaming-columns
注意:使用make:migration而不是migrate:make for Laravel 5.x.
因为没有任何答案有效,所以在这里投入0.02美元,但确实让我走上了正确的道路.发生的事情是先前的外部约束引发了错误.当你想到它时显而易见.
因此,在新的迁移up
方法中,首先删除原始约束,重命名列,然后使用新列名再次添加约束.在该down
方法中,您执行完全相反的操作,以便它返回到已售出的设置.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('proxy4s', function (Blueprint $table) {
// Drop it
$table->dropForeign(['server_id']);
// Rename
$table->renameColumn('server_id', 'linux_server_id');
// Add it
$table->foreign('linux_server_id')->references('id')->on('linux_servers');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('proxy4s', function (Blueprint $table) {
// Drop it
$table->dropForeign(['linux_server_id']);
// Rename
$table->renameColumn('linux_server_id', 'server_id');
// Add it
$table->foreign('server_id')->references('id')->on('linux_servers');
});
}
Run Code Online (Sandbox Code Playgroud)
希望这能在将来节省一些时间!
请分别按照以下步骤操作来重命名列迁移文件。
1-您的项目中是否有教义/ dbal库。如果您没有先运行命令
composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)
2-创建更新迁移文件以更新旧的迁移文件。警告(需要使用相同的名称)
php artisan make:migrate update_oldFileName_table
Run Code Online (Sandbox Code Playgroud)
例如我的旧迁移文件名:create_users_table更新文件名应为:update_users_table
3- update_oldNameFile_table.php
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
Run Code Online (Sandbox Code Playgroud)
“从”我的旧列名和“到”我的新列名
4-最后运行migrate命令
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
来源链接: laravel文档
归档时间: |
|
查看次数: |
69692 次 |
最近记录: |