Laravel 迁移表已经存在,但我想添加新的而不是旧的

新手上路,请多包涵

我之前创建了用户表。现在,我创建了一个新的迁移以在我的架构中创建一个新的 books 表。当我尝试运行命令时

php artisan migrate

表明:

 [Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre
ady exists (SQL: create table `users` (`id` int unsigned not null auto_incr
ement primary key, `username` varchar(255) not null, `email` varchar(255) n
ot null, `password` varchar(255) not null, `created_at` timestamp default 0
 not null, `updated_at` timestamp default 0 not null) default character set
 utf8 collate utf8_unicode_ci)

这是我的新迁移表:

 <?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBooksTable extends Migration {
    public function up()
    {
        Schema::create('books', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('auther');
            $table->string('area');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('books');
    }
}

我怎样才能摆脱错误?

原文由 MD. Atiqur Rahman 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 387
2 个回答

你需要跑

php artisan migrate:rollback

如果这也失败了,只需进入并删除您可能必须做的所有表,因为您的迁移表似乎被搞砸了,或者当您运行先前的回滚时您的用户表没有删除该表。

编辑:

发生这种情况的原因是您之前运行了回滚,并且代码中有一些错误或没有删除表。然而,这仍然会弄乱 laravel 迁移表,并且就它而言,您现在没有将用户表向上推的记录。但是,用户表确实已经存在,并且会抛出此错误。

原文由 eatingthenight 发布,翻译遵循 CC BY-SA 3.0 许可协议

检查 表名

Schema::create(’ table_name ‘, function (Blueprint $table)

原文由 Amar Suliman 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题