【问题标题】:laravel 8 migration error: Unknown column 'batch' in 'order clause' (SQL: select `migration` from `migrations` order by `batch` asc, `migration` asc)laravel 8迁移错误:“订单子句”中的未知列“批处理”(SQL:从“迁移”顺序中选择“迁移”按“批处理” asc,“迁移” asc)
【发布时间】:2021-07-19 13:02:00
【问题描述】:

[已解决]

我是 Laravel 和 PHP 的新手。

我尝试迁移自己的迁移,但我不能。

这是我的 laravel 和 php 信息:

我的 php 信息

  • PHP 8.0.3 (cli) (built: Mar 4 2021 05:33:14) ( NTS )
  • Composer version 2.0.11 2021-02-24
  • Laravel 8.x

mysql

  • Server version: 10.5.9-MariaDB Arch Linux

我的操作系统信息

  • 姓名:Linux ali 5.4.105-1-MANJARO #1 SMP PREEMPT 2021 x86_64 GNU/Linux

当我运行命令./artisan migratephp artisan migrate 时,Laravel 会抛出这个错误:

   Illuminate\Database\QueryException 

  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'batch' in 'order clause' (SQL: select `migration` from `migrations` order by `batch` asc, `migration` asc)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:687
    683▕         // If an exception occurs when attempting to run a query, we'll format the error
    684▕         // message to include the bindings with SQL, which will make this exception a
    685▕         // lot more helpful to the developer instead of just the database's errors.
    686▕         catch (Exception $e) {
  ➜ 687▕             throw new QueryException(
    688▕                 $query,
    689▕                 $this->prepareBindings($bindings),
    690▕                 $e
    691▕             );

  • A column was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`. 
    https://laravel.com/docs/master/migrations#running-migrations

      +28 vendor frames 
  29  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

无论我的迁移文件如何,它总是发生在我的系统上。 这意味着如果我在没有一个迁移文件的情况下迁移我的应用程序,则会出现此错误。

例如,我使用以下步骤创建一个项目:

composer create-project laravel/laravel mig "8.x"

这个问题在 8.x、7.x、6.x 版本上测试过。

cd mig

更改我的.env 文件并设置以下变量:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME="my database"
DB_PASSWORD="my pasword"

这是我的 .env 文件:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:J2//xlOZPYFlkzQwWmuwbILklCmPnTV6beXFED48K4I=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME="my database"
DB_PASSWORD="my pasword"

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

如果我输入了错误的密码或我的数据库和用户名,我将收到此错误:

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1045] Access denied for user 'database'@'localhost' (using password: NO) (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

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

      +33 vendor frames 
  34  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

但是当我写正确的数据库信息时,Laravel 总是抛出这个错误:


   Illuminate\Database\QueryException 

  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'batch' in 'order clause' (SQL: select `migration` from `migrations` order by `batch` asc, `migration` asc)

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

  • A column was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`. 
    https://laravel.com/docs/master/migrations#running-migrations

      +27 vendor frames 
  28  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

此错误和问题仅适用于迁移系统。 在我的应用 Query Builder 和 Eloquent Laravel 中运行良好。

在我的错误中它说:

• A column was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`.

但它不起作用,为什么?

解决方案

这是我的 mysql 表:

+-------------------+
| Tables_in_laravel |
+-------------------+
| migrations        |
+-------------------+

migrations 表已存在于我的数据库中,但它是一个自定义迁移表。 实际上这个表是由另一个框架创建的...... https://github.com/alirezaarzehgar/my-exercise-php-framework.git

我在许多 web 应用程序中使用 signal@localhost mysql 用户,此操作产生了冲突。

这是我的解决方案:

首先你应该删除你当前mysql用户的所有表。 在这种情况下,我只有migrations 表:

mysql -u username -p'password' databse -e "DROP TABLE migrations"

如果您想查看所有迁移,可以运行以下命令:

mysql -u username -p'password' databse -e "SHOW TABLES"

然后您可以删除所有这些表。

然后你就可以运行你的迁移了。 ./artisan migrate

【问题讨论】:

  • 您也可以发布您的迁移吗?
  • 我的问题与我的迁移无关,实际上即使我没有任何迁移。

标签: php sql-server laravel migration laravel-artisan


【解决方案1】:

更改您的“db_database”,希望您的问题能够解决

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=root
DB_PASSWORD=

【讨论】:

  • 这种方式行不通。我的问题是迁移表已经存在于我的数据库中。
  • 运行这个命令'php artisan migrate:rollback' 然后'php artisan migrate'
【解决方案2】:

删除旧的迁移表,然后重新运行 artisan migrate 看看会发生什么

【讨论】:

    【解决方案3】:

    首先你应该删除你当前mysql用户的所有表。 在这种情况下,我只有migrations 表:

    mysql -u username -p'password' databse -e "DROP TABLE migrations"

    如果您想查看所有迁移,可以运行以下命令:

    mysql -u username -p'password' databse -e "SHOW TABLES"

    然后您可以删除所有这些表。

    然后你就可以运行你的迁移了。 ./artisan migrate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-07
      • 2021-05-01
      • 2019-02-15
      • 2021-08-30
      • 2020-02-17
      • 1970-01-01
      • 2019-12-27
      • 2016-09-17
      相关资源
      最近更新 更多