迁移数据库时出错(laravel):表已存在

zi8p0yeb  于 2021-06-17  发布在  Mysql
关注(0)|答案(4)|浏览(591)

我已经安装了laravel的auth和chatter论坛包。当我尝试迁移数据库时,出现以下错误:

Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view alr eady exists: 1050 Table 'users' already exists (SQL: create table `users` (`id` int unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` va rchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb 4_unicode_ci')

  at C:\xampp\htdocs\Application\vendor\laravel\framework\src\Illuminate\Databas e\Connection.php:664

    660|         // If an exception occurs when attempting to run a query, we'll  format the error
    661|         // message to include the bindings with SQL, which will make th is exception a
    662|         // lot more helpful to the developer instead of just the databa se's errors.
    663|         catch (Exception $e) {
    664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 T able 'users' already exists")
      C:\xampp\htdocs\Application\vendor\laravel\framework\src\Illuminate\Databa se\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\Application\vendor\laravel\framework\src\Illuminate\Databa se\Connection.php:458

  Please use the argument -v to see more details.

我尝试使用以下命令进行迁移:

php artisan migrate
l2osamch

l2osamch1#

migrate:refresh 命令将回滚所有迁移,然后执行 migrate 命令。此命令可有效地重新创建整个数据库:

php artisan migrate:refresh

// Refresh the database and run all database seeds...
php artisan migrate:refresh --seed

另一个解决方案是:删除 users table 表中的用户条目 migrations table。
执行之后,运行migrate artisan命令: php artisan migrate

mbzjlibv

mbzjlibv2#

如果你检查错误跟踪,它几乎在底部说:
基表或视图已存在:1050 t表“users”已存在“)c:\xampp\htdocs\application\vendor\laravel\framework\src\illuminate\databa se\connection。php:458
这意味着 users 表已经存在,因此当您运行迁移时,它试图创建一个已经在数据库中创建的表,因此出现错误。
因此,要在再次运行迁移之前撤消迁移,可以执行以下操作:

php artisan migrate:refresh

检查有关回滚迁移的文档。
这将运行 down() 在实际运行 up() 一个。
如果你去你的 users 迁移,你可以看到 down() 函数,它应该如下所示:
数据库/迁移/x\u xx\u xx\u \u创建\u用户\u表.php

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('users');
}

无论何时创建迁移,都要实现 down() 方法,以使用回滚选项。

tktrz96b

tktrz96b3#

我相信这将工作,因为我有同样的问题,但我修复了这个

php artisan migrate:fresh

请检查一下
更新
删除项目并安装新的laravel项目
并在.env文件中进行更改
在database.php中

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => 'InnoDB',
    ],

将现有代码替换为
然后跑

php artisan make:auth 

 php artisan migrate

或者

php artisan migrate:fresh

我希望这次能成功
并确保在系统中安装了所有要求https://laravel.com/docs/5.7/installation

e0bqpujr

e0bqpujr4#

好吧,每次我安装拉维我都会遇到同样的错误。尝试更改迁移文件中的代码,如下所示:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email',64{ADD THIS PARAMETER})->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

您必须为唯一或键的字符串(最大长度)添加第二个参数,如email。别忘了清理数据库里的表格,所以当你跑步的时候 php artisan migrate 数据库已清除。

相关问题