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

时间:2019-01-03 13:51:26

标签: php mysql laravel

我已经安装了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

4 个答案:

答案 0 :(得分:2)

如果检查错误跟踪,它将几乎显示在底部:

  

基本表或视图已存在:1050个能力强大的“用户”   存在”)   C:\ xampp \ htdocs \ Application \ vendor \ laravel \ framework \ src \ Illuminate \ Databa   se \ Connection.php:458

这意味着users表已经存在,因此在运行迁移时,它试图创建一个已经在数据库中创建的表,因此会出现错误。

因此,在再次运行之前撤消此迁移,您可以这样做:

php artisan migrate:refresh

查看有关rolling back migrations的文档。

这将在实际运行down()之前运行已在系统中迁移的每个迁移文件的up()函数。

如果您要进行users迁移,则可以看到down()函数,它看起来应该像这样:

数据库/迁移/XXXX_XX_XX_XXXXXX_create_users_table.php

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

无论何时创建迁移,请始终实施down()方法,以使用回滚选项。

答案 1 :(得分:0)

好吧,总是我安装了laravel,我仍然遇到相同的错误。尝试像这样更改迁移文件中的代码:

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();
    });
}

您必须在唯一的字符串或键(例如电子邮件)中添加第二个参数(最大长度)。 不要忘记清除表中的数据库,因此当您运行php artisan migrate时,数据库将被清除。

答案 2 :(得分:0)

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

php artisan migrate:refresh

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

另一种解决方案是:从数据库中删除 users table 表也从 migrations 表中删除用户条目。

之后,执行运行migrate Artisan命令:php artisan migrate

答案 3 :(得分:0)

我确定这可以解决问题,因为我遇到了同样的问题,但是我已经解决了

 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

相关问题