gpt4 book ai didi

php - laravel 迁移 :rollback wrong

转载 作者:可可西里 更新时间:2023-11-01 07:51:13 25 4
gpt4 key购买 nike

Laravel 版本 5.1.43(LTS)

我在终端中使用 php artisan migrate:rollback 然后返回错误信息。但是数据库变了。然后我再次重新输入此命令,没有错误消息。

谁能帮我解决这个问题?

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP'user_id'; check that column/key exists (SQL: alter table crm_user drop index user_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.

关于php - laravel 迁移 :rollback wrong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40264158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com