Laravel 4 迁移回滚问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17174463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:59:34  来源:igfitidea点击:

Laravel 4 migrate rollback problems

phplaravel

提问by Dexty

I can easily run the artisan migrate etc, but when i try to roll it back, with migration:rollback i keep getting this error,

我可以轻松地运行工匠迁移等,但是当我尝试回滚它时,使用迁移:回滚我不断收到此错误,

c:\xampp\htdocs\laravel>php artisan migrate:rollback
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Class 'CreateCodesnippetsTable' not found","file":"C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illum
inate\Database\Migrations\Migrator.php","line":301}}

Is this a bug? or how should i debug this?

这是一个错误吗?或者我应该如何调试?

回答by DerLola

Maybe you've already solved this issue. But I'm noticing that for some reason a rollback often requires you to run composer dumpautoloadfirst. Even if your migration works.

也许你已经解决了这个问题。但我注意到出于某种原因,回滚通常需要您先运行composer dumpautoload。即使您的迁移有效。

回答by JamesG

Having just wrestled with this problem for several days, I think I can now provide the definitiveanswer to solving this problem. Yeah, big call I know, but bear with me.

刚刚与这个问题搏斗了几天,我想我现在可以提供解决这个问题的明确答案。是的,我知道很大,但请耐心等待。

The first port of call if you encounter this problem is to run composer dump-autoload. This should result in an updated version of the file vendor/composer/autoload_classmap.php.

如果您遇到此问题,首先要运行composer dump-autoload. 这应该会导致文件的更新版本vendor/composer/autoload_classmap.php

If autoload_classmap.phpdoesn't get updated then you may have a permissions problem, in which case you could try sudo composer dump-autoload.

如果autoload_classmap.php没有得到更新,那么您可能有权限问题,在这种情况下,您可以尝试sudo composer dump-autoload.

However, if autoload_classmap.phpdoesget updated, check that it contains an entry for your migration class (in this case CreateCodesnippetsTable). If there is no entry for this class then you should check your composer.jsonfile and make sure the app/database/migrationsfolder is included in the autoload section, eg:

但是,如果autoload_classmap.php确实得到更新,请检查它是否包含您的迁移类的条目(在本例中为CreateCodesnippetsTable)。如果没有该类的条目,那么您应该检查您的composer.json文件并确保该app/database/migrations文件夹包含在自动加载部分中,例如:

"autoload": {
    "classmap": [
        "app/controllers",
        "app/models",
        "app/database/migrations"
    ]
},

This last bit is what screwed things up for me. In a misguided attempt at optimising things I pulled as much as I could out of my composer.jsonfile, naively thinking this would only affect web requests. It turns out this affected Artisan as well, so putting this line back in and running composer dump-autoloadfixed the problem for me.

最后一点让我把事情搞砸了。在优化内容的误导尝试中,我尽可能多地从我的composer.json文件中提取,天真地认为这只会影响 Web 请求。事实证明这也影响了 Artisan,所以把这条线放回去并运行composer dump-autoload为我解决了这个问题。

Finally, if all that fails, then maybethere's a bug in one of the supporting libraries that is causing the problem, in which case you can try updating using composer updateor some variation thereof. However, I suspect that this will rarely be the true cause of the problem.

最后,如果所有这些都失败了,那么可能是支持库之一中存在导致问题的错误,在这种情况下,您可以尝试更新 usingcomposer update或其某些变体。但是,我怀疑这很少是问题的真正原因。

回答by bmnepali

If you are in windows, simply use composerin your terminal/command line utility and do the following:

如果您在 Windows 中,只需在终端/命令行实用程序中使用composer并执行以下操作:

composer dump-autoload

Hope it helps!

希望能帮助到你!

回答by Altrim

From what I can see I am guessing you have changed the class name manually. In the error you have the class name CreateCodesnippetsTablebut in the migration file you provided (pastebin), the class name is CreateCodeSnippetsTable(notice the S in Snippets, I guess that is what you changed manually).

从我所见,我猜你已经手动更改了类名。在错误中,您有类名,CreateCodesnippetsTable但在您提供的迁移文件(pastebin)中,类名是CreateCodeSnippetsTable(注意 Snippets 中的 S,我猜这是您手动更改的)。

If you check the migrations table in your database you will see records for each migration. When you create the migration it will be saved in the database with that name and the rollback method tries to read the file with the name provided in the database, in the case when you change it manually laravel can't find the class and you get the error.

如果您检查数据库中的迁移表,您将看到每次迁移的记录。当您创建迁移时,它将以该名称保存在数据库中,并且回滚方法尝试使用数据库中提供的名称读取文件,如果您手动更改它,laravel 找不到该类并且您得到错误。

To fix this you can undo the changes and try to rollback or manually edit the migration row in your database to include the correct class name.

要解决此问题,您可以撤消更改并尝试回滚或手动编辑数据库中的迁移行以包含正确的类名。

Hope this helps.

希望这可以帮助。

回答by Ahmad Sharif

It seems to me there are no single solution of this error. I have tried many suggestion but at last this one works in my end.

在我看来,这个错误没有单一的解决方案。我尝试了很多建议,但最后这个建议对我有用。

COMPOSER=composer.json composer dump-autoload

enter image description here

在此处输入图片说明

回答by Grasshopper

I fixed it by running

我通过运行修复了它

composer.phar update

composer.phar update

回答by abu adeel

download the composer.pharfile from laravel site and bring the composer.pharfile to the root directoryof laravel folder,

从 laravel 站点下载composer.phar文件并将composer.phar文件带到laravel 文件夹的根目录

then from terminal come to the root directory of laravel and run the composer.phar updateor simply run php artisan dump-autoload.

然后从终端来到 laravel 的根目录并运行composer.phar update或简单地运行php artisan dump-autoload.

回答by Yehia

i faced the same problem and figure out the problem

我遇到了同样的问题并找出了问题所在

I've created a migration for adding new column date in PatientReasonOfVisits table i used laravel generators when i create the migration the class name was

我创建了一个迁移,用于在 PatientReasonOfVisits 表中添加新列日期我在创建迁移时使用了 laravel 生成器,类名是

class AddDateToPatientReasonOfVisitsTable

sure after creating a new migration file u need to run composer dump-autoload to ensure the file is listed in class map file

确保在创建新的迁移文件后,您需要运行 composer dump-autoload 以确保该文件在类映射文件中列出

the file name was 2014_09_02_214134_add_date_to_patientreasonofvisitstable.php

文件名为 2014_09_02_214134_add_date_to_patientreasonofvisitstable.php

the migration done successfully and a new record has been added into migration table. in migration column the file name is used

迁移成功完成,新记录已添加到迁移表中。在迁移列中使用文件名

when i rollback the migration i got the class not found exception what class is not found this one

当我回滚迁移时,我得到了找不到类的异常,找不到这个类

AddDateToPatientreasonofvisitsTable

note : the difference between classes names

注意:类名的区别

why and how i solved this problem i guess when u rollback the class name resolved using the migration file name which in migration table the capital and small letters is decided by underscores "_" in file name

我为什么以及如何解决这个问题我猜当你回滚使用迁移文件名解析的类名时,迁移表中的大写和小写字母由文件名中的下划线“_”决定

so after renaming the migration file to 2014_09_02_214134_add_date_to_patient_reason_of_visits_table.php and sure run composer dump-autoload after renaming the file the class name resolved correctly with no exception

因此,在将迁移文件重命名为 2014_09_02_214134_add_date_to_patient_reason_of_visits_table.php 并确保在重命名文件后运行 composer dump-autoload 后,类名正确解析,无一例外

回答by idro2k

I simply dropped the migrations table then ran php artisan migrate:refresh

我只是删除了迁移表然后运行 php artisan migrate:refresh

Then the migrations were all able to execute, not sure if that's the best method, but it worked for me.

然后迁移都能够执行,不确定这是否是最好的方法,但它对我有用。

I'm running Laravel 5.

我正在运行 Laravel 5。