【问题标题】:我在 Laravel Table Migrate 上遇到错误
【发布时间】:2022-01-23 11:21:16
【问题描述】:

我是 laravel 新手,我是初学者,我在 laravel 表中运行 php artisan migrate 时遇到问题。

C:\wamp64\www\my-blog>php artisan migrate
Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at C:\wamp64\www\my-blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:703
    699▕         // If an exception occurs when attempting to run a query, we'll format the error
    700▕         // message to include the bindings with SQL, which will make this exception a
    701▕         // lot more helpful to the developer instead of just the database's errors.
    702▕         catch (Exception $e) {
  ➜ 703▕             throw new QueryException(
    704▕                 $query, $this->prepareBindings($bindings), $e
    705▕             );
    706▕         }
    707▕     }

  1   C:\wamp64\www\my-blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
      PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists")

  2   C:\wamp64\www\my-blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
      PDOStatement::execute()

【问题讨论】:

  • 错误很明显。您正在尝试创建一个已存在于数据库中的表。
  • 如果您之前运行过迁移,则需要运行php artisan migrate:freshphp artisan migrate:refresh 或运行php artisan migrate:rollback,然后运行php artisan migrate

标签: laravel database error-handling


【解决方案1】:

我认为错误信息很清楚,但是您有几种不同的方法来解决它。

  1. 重新运行迁移:

    php 工匠迁移:刷新

  2. 回滚迁移:

    php 工匠迁移:回滚

当您使用 PHPStorm 时,您的窗口右侧有一个数据库选项卡,您可以在那里手动删除用户表,然后运行 ​​php artisan migrate - 但是我建议使用 1. 或 2. 而不是手动执行

【讨论】:

    【解决方案2】:

    快跑

    php 工匠迁移:回滚

    【讨论】:

      猜你喜欢
      • 2016-10-14
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多