Php 带主键的Laravel迁移删除列(错误)
我已经检查了其他主题,但没有解决方案。我上次试过:Php 带主键的Laravel迁移删除列(错误),php,sql,laravel,Php,Sql,Laravel,我已经检查了其他主题,但没有解决方案。我上次试过: public function up() { if(Schema::hasColumn('orders_products_variations','id')) { Schema::table('orders_products_variations',function (Blueprint $table) { $table->unsignedInteger('id')->change()
public function up()
{
if(Schema::hasColumn('orders_products_variations','id')) {
Schema::table('orders_products_variations',function (Blueprint $table) {
$table->unsignedInteger('id')->change();
$table->dropPrimary();
$table->dropColumn('id');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('id');
});
}
错误消息是:
SQLSTATE[42000]:语法错误或访问冲突:1075不正确的表定义;只能有一个自动列,必须将其定义为键(SQL:alter tableorders\u products\u variations
drop主键)
有什么想法吗
迁移类包含两个方法:up和down。up方法是
用于向数据库中添加新表、列或索引,而
down方法应反转up执行的操作
方法
删除列
要删除列,请使用Schema builder inside down方法上的dropColumn
添加/删除列
PS当您必须向Laravel中的现有表中添加新列时,您必须
创建新的迁移
听起来像是
id
被定义为自动递增,所以它必须是主键。您应该能够在不使用任何其他环的情况下删除id列。我已经尝试只删除id列,但没有效果。您可能需要将其拆分为两个Schema::table
调用。第一个将删除主键,而第二个将删除整个列。Thx但我也尝试了您尝试删除的Wich列?让我们在chat中继续讨论这是完全错误的up()
不仅仅用于添加,而down()
不仅仅用于删除。OP的问题就是一个例子。@Don't你可以参考Hmm,这很有趣:D但是我仍然认为这是完全错误的,OP的问题是一个完美的例子,我也一直在这样做。模式发生变化,有时您需要删除列—除了迁移之外,您将如何执行此操作?我以前从未使用过up()方法来删除列。然而,由于在运行php artisan migrate:rollback时调用了down()方法,所以我使用它来删除列、表。。。
if (Schema::hasColumn('table_name', 'column_name')) {
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name');
});
}
php artisan make:migration add_column_to_table