php Laravel 迁移表已经存在,但我想添加新的而不是旧的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26077458/
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
Laravel Migration table already exists but i want to add new not the older
提问by MD. Atiqur Rahman
I previously created users table. Now I have created a new migration to create a new books table inside my schema. When I try to run the command
我之前创建了用户表。现在我创建了一个新的迁移来在我的架构中创建一个新的 books 表。当我尝试运行命令时
php artisan migrate
It shows:
表明:
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre
ady exists (SQL: create table `users` (`id` int unsigned not null auto_incr
ement primary key, `username` varchar(255) not null, `email` varchar(255) n
ot null, `password` varchar(255) not null, `created_at` timestamp default 0
not null, `updated_at` timestamp default 0 not null) default character set
utf8 collate utf8_unicode_ci)
Here is my new migration table:
这是我的新迁移表:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration {
public function up()
{
Schema::create('books', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('auther');
$table->string('area');
$table->timestamps();
});
}
public function down()
{
Schema::drop('books');
}
}
How can I get rid of the error?
我怎样才能摆脱错误?
采纳答案by mschuett
You need to run
你需要跑
php artisan migrate:rollback
if that also fails just go in and drop all the tables which you may have to do as it seems your migration table is messed up or your user table when you ran a previous rollback did not drop the table.
如果这也失败了,只需进入并删除您可能必须执行的所有表,因为看起来您的迁移表被搞砸了,或者您运行上一次回滚时的用户表没有删除该表。
EDIT:
编辑:
The reason this happens is that you ran a rollback previously and it had some error in the code or did not drop the table. This still however messes up the laravel migration table and as far as it's concerned you now have no record of pushing the user table up. The user table does already exist however and this error is throw.
发生这种情况的原因是您之前运行了回滚并且它在代码中有一些错误或没有删除表。然而,这仍然会弄乱 Laravel 迁移表,就它而言,您现在没有将用户表向上推的记录。但是,用户表确实已经存在,并且会抛出此错误。
回答by amrography
In v5.x, you might still face the problem. So, try to delete related table manually first using
在 v5.x 中,您可能仍会遇到该问题。因此,尝试首先使用手动删除相关表
php artisan tinker
php artisan tinker
Then
然后
Schema::drop('books')
Schema::drop('books')
(and exit with q
)
(并退出q
)
Now, you can successfully php artisan migrate:rollback
and php artisan migrate
.
现在,您可以成功php artisan migrate:rollback
并php artisan migrate
.
If this happens repeatedly you should check that the down()
method in your migration is showing the right table name. (Can be a gotcha if you've changed your table names.)
如果这种情况反复发生,您应该检查down()
迁移中的方法是否显示了正确的表名。(如果您更改了表名,可能会遇到问题。)
回答by Abaza
I had the same trouble. The reason is that your file namein migrations folder does not match with name of migrationin your database (see migrations table). They should be the same.
我有同样的麻烦。原因是迁移文件夹中的文件名与数据库中的迁移名称不匹配(请参阅迁移表)。他们应该是一样的。
回答by user8810865
You can use
php artisan migrate:fresh
to drop all tables and migrate then.
Hope it helps
您可以使用
php artisan migrate:fresh
删除所有表然后迁移。希望能帮助到你
回答by Trikly
Also u may insert befor
Schema::create('books', function(Blueprint $table)
the following code Schema::drop('books');
您也可以插入
Schema::create('books', function(Blueprint $table)
以下代码Schema::drop('books');
回答by maksbd19
EDIT: (for laravel)
编辑:(对于laravel)
Just Came across this issue while working on a project in laravel. My tables were messed up, needed to frequent changes on columns. Once the tables were there I wasn't able to run php artisan migrate
anymore.
刚刚在 laravel 中处理一个项目时遇到了这个问题。我的表搞砸了,需要频繁更改列。一旦桌子在那里,我就不能再跑php artisan migrate
了。
I've done following to get rid of the issue-
我已经做了以下工作来解决这个问题-
- Drop the tables in the database [every one, including the migration table]
$ composer dump-autoload -o
php artisan migrate
- 删除数据库中的表【每一张,包括迁移表】
$ composer dump-autoload -o
php artisan migrate
Previous Comment, regarding lumen
以前的评论,关于流明
[Well, pretty late to the party (and possibly a different party than what I was looking for). I banged my head, screamed out loud and by the grace of gray skull just found a solution.]
[好吧,聚会很晚了(可能是与我想要的聚会不同的聚会)。我敲了敲头,大声尖叫,靠着灰色骷髅的恩典,我找到了解决办法。]
I'm developing an restful app using lumen and I'm new to it. This is my first project/experiment using laraval and lumen. My dependencies-
我正在使用 lumen 开发一个安静的应用程序,我是新手。这是我使用 laraval 和 lumen 的第一个项目/实验。我的依赖-
"require": {
"php": ">=5.6.4",
"laravel/lumen-framework": "5.4.*",
"vlucas/phpdotenv": "~2.2",
"barryvdh/laravel-cors": "^0.8.6",
"league/fractal": "^0.13.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"phpunit/phpunit": "~5.0",
"mockery/mockery": "~0.9.4"
}
Anyway, everything was fine until yesterday night but suddenly phpunit
started complaining about an already existed table.
无论如何,直到昨天晚上一切都很好,但突然phpunit
开始抱怨已经存在的桌子。
Caused by
PDOException: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'items' already exists
Duh! Items
table should exist in the database, or else how am i supposed to save items!
呸!Items
表应该存在于数据库中,否则我应该如何保存项目!
Anyway the problem only persisted in the test classes, but strangely not in the browser (I checked with chrome, firefox and postman altering headers). I was getting JSON responses with data as expected.
无论如何,问题只存在于测试类中,但奇怪的是不在浏览器中(我使用 chrome、firefox 和 postman 更改标题进行了检查)。我按预期收到了带有数据的 JSON 响应。
I dropped the database and recreated it with lots of migrate
, refresh
, rollback
. Everything was fine but in phpunit
.
我放弃了数据库,并有大量的重建它migrate
,refresh
,rollback
。一切都很好,但在phpunit
.
Out of desperation I removed my migration files (of course I took a backup first ) then hit phpunit
in the terminal. Same thing all over again.
出于绝望,我删除了我的迁移文件(当然我先备份了)然后点击phpunit
了终端。同样的事情又来了。
Suddenly I remembered that I put a different database name in the phpunit.xml
file for testing purpose only. I checked that database and guess what! There was a table named items
. I removed this table manually, run phpunit
, everything started working just fine.
突然我想起我在phpunit.xml
文件中放了一个不同的数据库名称只是为了测试目的。我检查了那个数据库,猜猜是什么!有一张名为items
. 我手动删除了这个表,运行phpunit
,一切开始正常工作。
I'm documenting my experience for future references only and with hope this might help someone in future.
我正在记录我的经验,仅供将来参考,希望这可能对将来的人有所帮助。
回答by Nik K
I inherited some real bad code from someone who wasn't using migrations!?, so manually pasted the filenames into the migrations, forgetting to remove the trailing .php
我从没有使用迁移的人那里继承了一些真正糟糕的代码!?,所以手动将文件名粘贴到迁移中,忘记删除尾随的 .php
So that caused the 'table exists' error despite the filename and migration matching.
因此,尽管文件名和迁移匹配,但仍会导致“表存在”错误。
2018_05_07_142737_create_users_table.php - WRONG 2018_05_07_142737_create_users_table - CORRECT
2018_05_07_142737_create_users_table.php - 错误 2018_05_07_142737_create_users_table - 正确
回答by Murad
You can delete all the tables but it will be not a good practice, instead try this command
您可以删除所有表,但这不是一个好习惯,请尝试使用此命令
php artisan migrate:fresh
Make sure to use the naming convention correctly. I have tested this in version laravel 5.7 It is important to not try this command when your site is on server because it drops all information.
确保正确使用命名约定。我已经在 laravel 5.7 版本中对此进行了测试。当您的站点在服务器上时不要尝试此命令,因为它会丢失所有信息,这一点很重要。
回答by Martijn van der Bruggen
I had a similar problem after messing with foreign key constraints. One of my tables (notes) was gone and one kept coming back (tasks) even after dropping it in MySQL, preventing me from running: php artisan migrate/refresh/reset
, which produced the above 42s01 exception.
在搞乱外键约束后,我遇到了类似的问题。我的一张表(笔记)不见了,即使将它放入 MySQL 后,它仍然会回来(任务),阻止我运行: php artisan migrate/refresh/reset
,这产生了上述 42s01 异常。
What I did to solve it was ssh into vagrant then go into MySQL (vagrant ssh, mysql -u homestead -p secret
), then: DROP DATABASE homestead; Then CREATE DATABASE homestead; Then exit mysql and run:
php artisan migrate`.
我为解决这个问题所做的是 ssh 进入 vagrant,然后进入 MySQL ( vagrant ssh, mysql -u homestead -p secret
),然后:DROP DATABASE homestead; Then CREATE DATABASE homestead; Then exit mysql and run:
php artisan migrate`。
Obviously this solution will not work for people not using vagrant/homestead. Not claiming in any way this is a proper workflow but it solved my problem which looks a lot like the above one.
显然,此解决方案不适用于不使用 vagrant/homestead 的人。没有以任何方式声称这是一个正确的工作流程,但它解决了我的问题,看起来很像上面的问题。
回答by Ayd?n Bulut
I think my answer will help more. I faced this error also. Then I deleted specific migration file and tried to recreate by php artisan.
我想我的回答会更有帮助。我也遇到了这个错误。然后我删除了特定的迁移文件并尝试通过 php artisan 重新创建。
But before get this point 1 or 2 days ago while I was watching laracast videos about migation, I was thinking rollback and migrate specific table. For some reason I deleted specific migration file and tried recreate but doing so I got:
但是在 1 或 2 天前,当我观看有关迁移的 laracast 视频时,我正在考虑回滚和迁移特定表。出于某种原因,我删除了特定的迁移文件并尝试重新创建,但这样做我得到:
[ErrorException] include(C:\wamp64\www\laraveldeneme\vendor\composer/../../database/migrations/2017_01_09_082715_create_articles_table.php): failed to open stream: No such file or directory
[ErrorException] include(C:\wamp64\www\laraveldeneme\vendor\composer/../../database/migrations/2017_01_09_082715_create_articles_table.php):无法打开流:没有这样的文件或目录
When I check that file I saw the line below at top of array in the autoload_classmap.php file:
当我检查该文件时,我在 autoload_classmap.php 文件中的数组顶部看到了下面的行:
'CreateArticlesTable'=>$baseDir.'/database/migrations/2017_01_09_083946_create_articles_table.php',
'CreateArticlesTable'=>$baseDir.'/database/migrations/2017_01_09_083946_create_articles_table.php',
Altough rollback or delete a migration file, the record related with the migration file remains in the composer autoload_classmap.php file.
尽管回滚或删除迁移文件,但与迁移文件相关的记录仍保留在 composer autoload_classmap.php 文件中。
To solve this problem, I have found composer command below from somewhere I can't remember.
为了解决这个问题,我从我不记得的地方找到了下面的 composer 命令。
composer dump-autoload
When I rand this code, the line related with the migration file that I deleted is gone. Then I ran:
当我 rand 这段代码时,与我删除的迁移文件相关的行消失了。然后我跑了:
php artisan make:migration create_articles_table --create=articles
Finally I recreated my migration file with same name
最后我重新创建了同名的迁移文件