laravel 在laravel中使用迁移删除数据库中的主键和自动增量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44451230/
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
Remove primary key and auto-increment in database using migration in laravel
提问by Vishal B
I have table with primary key and auto-increment field, I want make new migration to drop primary key index and also remove the auto increment field. How can i achieve this.
我有主键和自动增量字段的表,我想进行新迁移以删除主键索引并删除自动增量字段。我怎样才能做到这一点。
I created new migration as
我创建了新的迁移
public function up()
{
Schema::table('tbl_message_read_state', function (Blueprint $table) {
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tbl_message_read_state', function (Blueprint $table) {
$table->dropPrimary('message_id');
$table->unsignedInteger('message_id');
});
}
It gave me error in command as [Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'message _id' (SQL: alter table tbl_'message_read_state' add 'message_id' int unsigned not null)
它给了我命令错误 [Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'message _id' (SQL: alter table tbl_'message_read_state' add 'message_id' int unsigned not null)
Whats Wrong ?????
怎么了 ?????
回答by Rahul
Blueprint class offers dropPrimary methods that allow you to remove primary key.
Blueprint 类提供允许您删除主键的 dropPrimary 方法。
public function down()
{
Schema::table('table', function (Blueprint $table) {
$table->dropPrimary();
$table->unsignedInteger('id'); // for removing auto increment
});
}
回答by sturrockad
The dropPrimary
method only removed the primary key, not the column. You would have to do:
该dropPrimary
方法只删除了主键,而不是列。你必须这样做:
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tbl_message_read_state', function (Blueprint $table) {
$table->dropPrimary('message_id');
$table->dropColumn('message_id');
$table->unsignedInteger('message_id');
});
}
Or instead of dropping and re-creating the column, you can use change
in Laravel 5.
或者,您可以change
在 Laravel 5 中使用,而不是删除和重新创建列。
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tbl_message_read_state', function (Blueprint $table) {
$table->dropPrimary('message_id');
$table->integer('message_id')->unsigned()->change();
});
}
回答by Vishal Varshney
You can try this
你可以试试这个
$table->dropPrimary('id_primary');
回答by Kimsal San
Use a simple drop column
使用简单的下拉列
$table->dropColumn('id');
$table->dropColumn('id');
回答by Adam
This did it for me:
这为我做到了:
Schema::table('table_name', function (Blueprint $table) {
// Make AI field `id` unsigned otherwise SQL
// will throw error when you try to remove it
$table->integer('id')->unsigned()->change();
$table->dropColumn('id');
// If there was a foreign on message_id, make sure to remove it
$table->dropForeign('table_name_message_id_foreign');
$table->dropPrimary('message_id');
$table->dropColumn('message_id');
}
回答by Vardges Qeshishyan
https://laravel.com/docs/4.2/schema#dropping-indexes
https://laravel.com/docs/4.2/schema#dropping-indexes
- tabel_name
- column
index $table->dropPrimary('users_id_primary');
- 表名
- 柱子
index $table->dropPrimary('users_id_primary');