小编典典

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

php

我之前创建了用户表。现在,我创建了一个新的迁移以在我的架构中创建一个新的 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');
    }
}

我怎样才能摆脱错误?


阅读 104

收藏
2022-07-25

共1个答案

小编典典

在 v5.x 中,您可能仍然会遇到问题。因此,尝试首先使用手动删除相关表

php artisan tinker

然后

Schema::drop('books')

(并退出q

现在,您可以成功地php artisan migrate:rollbackphp artisan migrate.

如果这种情况反复发生,您应该检查down()迁移中的方法是否显示了正确的表名。(如果您更改了表名,可能会遇到问题。)

2022-07-25