【问题标题】:Why i am gettng error while migrating the database [duplicate]为什么在迁移数据库时出现错误 [重复]
【发布时间】:2020-02-04 01:12:04
【问题描述】:

在复制此项目之前一切正常,但在将此项目复制到另一台笔记本电脑后,我无法迁移数据库错误

   Illuminate\Database\QueryException  : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'discussionforum.channels' doesn't exist (SQL: select * from `channels`)

  at /opt/lampp/htdocs/discussionforum/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669| 

  Exception trace:

  1   Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))
      [internal]:0

  2   PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'discussionforum.channels' doesn't exist")
      /opt/lampp/htdocs/discussionforum/vendor/laravel/framework/src/Illuminate/Database/Connection.php:327

  Please use the argument -v to see more details.

【问题讨论】:

  • 尝试使用“php artisan migrate:fresh”并说出结果
  • 您确定您的表迁移的日期是按照您希望配置的顺序(从最旧到最新)进行的吗?听起来您可能在创建表之前进行了修改(添加、删除或更改字段或索引)。将任何索引拆分为在创建所有表后运行的自己的迁移也可能会有所帮助。
  • 用这么少的信息很难看出错误的位置。尝试添加相关的迁移类及其名称。看来您执行它们的顺序错误。
  • 您应该检查 AppServiceProvider 或其他在迁移之前启动的提供程序,如果您在提供程序中使用某些表或模型,当该表不存在时您无法使用迁移。

标签: laravel


【解决方案1】:

如果您不需要保留当前数据 您可以尝试使用

重置您的数据
php artisan migrate:refresh --seed

和/或删除并重新创建您的 “数据库。(sql扩展名)”文件

【讨论】:

  • 此方法无效
【解决方案2】:

您应该检查AppServiceProvider 或其他在迁移之前启动的提供程序,如果您在提供程序中使用某些表或模型,当该表不存在时您无法使用迁移。

如果你看起来更好有错误

1   Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))
      [internal]:0 

在任何表存在之前检查使用模型的提供者。

如果你坚持在提供者中使用模型,你应该在使用之前检查它是否存在,像这样:

Schema::hasTable('mytable'); // off course schema must be imported.

如果你发布你是提供者的内容,它会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 2019-02-14
    • 2020-11-28
    • 2017-02-21
    • 2016-04-08
    • 2021-02-28
    • 1970-01-01
    • 2013-05-12
    • 2012-10-31
    相关资源
    最近更新 更多