【问题标题】:Laravel migration is not working and missing `migrations` tableLaravel 迁移不起作用并且缺少“迁移”表
【发布时间】:2021-09-26 08:09:34
【问题描述】:

如果我运行 php artisan migrate 会失败

未找到基表或视图:1146 表 '*.migrations' 不存在。

而数据库是空的。

如果我运行php artisan migrate:install,我会看到迁移表,但它是空的。

DB 显示迁移表,但它是空的。

如果我再次运行php artisan migrate,则数据库再次为空,我得到同样的错误:

未找到基表或视图:1146 表 '*.migrations' 不存在。

也许有人知道这里发生了什么。

【问题讨论】:

  • 您要迁移新的项目迁移文件还是...?
  • 是的,表users、password_reset和failed_jobs已经有一些迁移文件。
  • AppServiceProvider 中的 boot() 方法中是否有任何内容或路由文件中的任何模型执行(作为闭包)?
  • 不,register() 和 boot() 是空的。
  • 当我删除database/mysql-schema.dump并通过pypMyAdmin从数据库中删除表并运行php artisan migrate然后我得到Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table 'users' add unique 'users_email_unique'('email'))

标签: php laravel


【解决方案1】:

为了解决这个问题,我需要将以下代码添加到app/Providers/AppServiceProvider.php:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;  # Added this Line

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);  # Added this Line
    }
}

运行php artisan migrate:fresh最终结果:

Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (14.68ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (12.33ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (18.95ms)

来源:Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

非常感谢 Weber 和 mathijing!

【讨论】:

    【解决方案2】:

    可能是几件事:

    • 试试php artisan migrate:reset
    • 您的迁移文件可能有问题:check here

    【讨论】:

    • php artisan migrate:reset 显示Nothing to rollback.。然后php artisan migrate 带来同样的错误SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists。 php artisan optimize:clear 也没什么区别。 Compiled views cleared! Application cache cleared! Route cache cleared! Configuration cache cleared! Compiled services and packages files removed! Caches cleared successfully!
    • php artisan migrate:fresh 也因Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table 'users' add unique 'users_email_unique'('email')) 而失败
    • 然后我会尝试添加 'Schema::defaultStringLength(191);'到您的 AppServiceProvider 文件。喜欢这里:stackoverflow.com/a/42245921/13146915
    • 神圣的**。那行得通! php artisan migrate:fresh 导致Dropped all tables successfully. Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (14.68ms) Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (12.33ms) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (18.95ms)
    猜你喜欢
    • 2017-06-18
    • 2017-05-27
    • 2018-06-18
    • 2021-02-08
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多