Laravel 迁移更改表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33888599/
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 to change table name
提问by HKumar
I want to change my two table name in the Laravel, so do I have to manually change the table name or it can be possible through migration.
我想在 Laravel 中更改我的两个表名,所以我必须手动更改表名还是可以通过迁移来实现。
回答by Gaz_Edge
To change a table name, you can do this:
要更改表名,您可以执行以下操作:
Schema::rename($currentTableName, $newTableName);
You can use the drop
or dropIfExists
methods to remove an existing table:
您可以使用drop
或dropIfExists
方法删除现有表:
Schema::drop('users');
Schema::dropIfExists('users');
Just add that to a migration and it should work.
只需将其添加到迁移中,它应该可以工作。
回答by Yevgeniy Afanasyev
You can rename table like that
您可以像这样重命名表
Schema::rename('old_table', 'new_table');
BUTbe careful if you have foreign keys
, indexes
and unique-s
.
但是,如果您有foreign keys
,indexes
和,请小心unique-s
。
you will not be able to deleted them after renaming, like thiat
重命名后您将无法删除它们,例如 thiat
Schema::table('new_table', function (Blueprint $table) {
$table->dropForeign(['transaction_id']);
});
because they will have old names and these names have table name in them.
因为它们将具有旧名称并且这些名称中包含表名称。
Thus, I recommend deleting foreign keys
and other stuff first
因此,我建议先删除foreign keys
和其他内容
Schema::table('old_table', function (Blueprint $table) {
$table->dropForeign(['transaction_id']);
});
Schema::rename('old_table', 'new_table');
Schema::table('new_table', function (Blueprint $table) {
$table->foreign('transaction_id')->references('id')->on('transactions');
});
回答by Nikunj K.
To rename an existing database table, use the rename method:
要重命名现有的数据库表,请使用重命名方法:
Schema::rename($from, $to);
To drop an existing table, you may use the drop or dropIfExists
methods:
要删除现有表,您可以使用 drop 或dropIfExists
方法:
Schema::drop('users');
Schema::dropIfExists('users');
回答by Saurabh Kataria
Firstly, use CLI command to create a migration:
首先,使用 CLI 命令创建迁移:
php artisan make:migration rename_table
Now, in the up method of the new migration class, use the rename method to change table name:
现在,在新迁移类的 up 方法中,使用 rename 方法更改表名:
Schema::rename('old_table_name', 'new_table_name');
Next, execute the migration command:
接下来,执行迁移命令:
php artisan migrate