Laravel版本5.1.43(LTS)
我使用PHP artisan migrate:在终端回滚然后返回错误消息.但数据库已更改.然后我再次重新输入此命令,没有错误消息.
任何人都可以帮我解决这个问题吗?
[Illuminate\Database\QueryException]
sqlSTATE[42000]: Syntax error or access violation: 1091 Can’t DROP’user_id’; check that column/key exists (sql: alter tablecrm_user
drop indexuser_id
)[PDOException]
sqlSTATE[42000]: Syntax error or access violation: 1091 Can’t DROP ‘user_id’; check that column/key exists
我的迁移代码
public function down()
{
if (Schema::hasColumn('crm_user', 'user_id')) {
Schema::table('crm_user', function (Blueprint $table) {
$table->dropColumn('user_id');
$table->dropIndex('user_id');
});
}
}
解决方法:
删除列时会自动删除索引.因此,当您尝试单独删除索引时,您会收到不存在的错误.
因此,要么交换订单,要先删除索引:
$table->dropIndex('user_id');
$table->dropColumn('user_id');
或者只是删除列,不要担心索引.
从MysqL手册:
If columns are dropped from a table, the columns are also removed from any index of which they are a part. If all columns that make up an index are dropped, the index is dropped as well.