php PHP工匠迁移不创建新表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36492917/
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
PHP artisan migrate not creating new table
提问by Prabin Parajuli
I am new to Laravel
and I'm following Laravel Documentations
as well as few Tutorial Videos. However, I am running this php artisan migrate
code in my local CMD prompt
and it's not creating Database Table
in phpmyadmin
. There are other few similar topics related to this in stackoverflow
but none solved my issue. Please don't mark this duplicate.
我是新手Laravel
,我正在关注Laravel Documentations
以及一些教程视频。但是,我php artisan migrate
在本地运行此代码CMD prompt
并且它不是Database Table
在phpmyadmin
. 还有其他一些与此相关的类似主题,stackoverflow
但没有一个解决了我的问题。请不要标记此重复项。
Ok, it goes like this. I run this code
好的,事情是这样的。我运行这段代码
php artisan make:migration create_student_table --create=student
and new file is created in migration folder as 2016_04_08_061507_create_student_table.php
并在迁移文件夹中创建新文件作为 2016_04_08_061507_create_student_table.php
Then in that file I run this code
然后在那个文件中我运行这个 code
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStudentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('student', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 20);
$table->string('email', 255);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('student');
}
}
Then in cmd
I run php artisan migrate
but it didn't create student
table. Instead it's showing this message
然后在cmd
我运行php artisan migrate
但它没有创建student
表。相反,它显示此消息
[PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
[PDOException] SQLSTATE[42S01]:基表或视图已经存在:1050 表“用户”已经存在
I created users
table few days ago using similar method as above. But new student
table is not being created. Am I missing anything here?
Thanks in advance.
users
几天前我使用与上面类似的方法创建了表格。但是student
没有创建新表。我在这里错过了什么吗?提前致谢。
回答by Jeff
That means Laravel is trying to run the users
table migration first. If you are in development and don't need to keep your data, you can just delete the users
table and then run php artisan migrate
这意味着 Laravel 试图首先运行users
表迁移。如果你正在开发中并且不需要保留你的数据,你可以删除users
表然后运行php artisan migrate
回答by aimme
EDIT
编辑
move out already ran migrations from database\migrations folder, before running new migration files. after running the new migrations move back in the past migrations where it was before.
在运行新的迁移文件之前,从 database\migrations 文件夹中移出已经运行的迁移。运行新迁移后,将移回之前的迁移。
That means you have already ran php artisan migrate
once and the table is already present in the database. sometimes you need to do a composer dump-autoload
if the artisan is lying.
这意味着您已经运行了php artisan migrate
一次并且该表已经存在于数据库中。composer dump-autoload
如果工匠在撒谎,有时你需要做一个。
so you need to either rollback the last change before editing and running php artisan migrate
. to rollback you could use php artisan migrate:rollback
因此您需要在编辑和运行之前回滚上次更改php artisan migrate
。回滚你可以使用phpartisan migrate:rollback
Also if you want to remove all changes you could run php artisan migrate:reset
.
此外,如果您想删除所有更改,您可以运行php artisan migrate:reset
.
There is php artisan migrate --force
to force run the migration forcefully.
有php artisan migrate --force
强制运行迁移强制。
After doing all these steps and if not able to run migration, if its the development environment, drop the database create database again and run the php artisan migrate
.
完成所有这些步骤后,如果无法运行迁移,如果是开发环境,则再次删除数据库创建数据库并运行php artisan migrate
.
回答by hiltondeville
I have been having a similar problem with my Laravel setup (mac os Sierra 10.12.16) and using MAMP with atom and have not found a definitive solution until I followed the next steps.
我的 Laravel 设置(mac os Sierra 10.12.16)和将 MAMP 与 atom 一起使用时遇到了类似的问题,并且在我执行后续步骤之前还没有找到明确的解决方案。
When setting up up my local environment I have found using the following steps has prevented the migration issues later on,
在设置我的本地环境时,我发现使用以下步骤可以防止以后出现迁移问题,
IN AppServiceProvider.php add the following code:
use Illuminate\Support\Facades\Schema;
public function boot()
{
//
Schema::defaultStringLength(191);
}
then in database.php:
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
THEN FINALLY IN .env:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=
DB_DATABASE=hackable
DB_USERNAME=root
DB_PASSWORD=root
Hope this helps someone as it took me days to finally get the setup correct :)
希望这对某人有所帮助,因为我花了几天时间才最终使设置正确:)
回答by Kayo Hamid
Your database already have this table, just drop it and let laravel create by artisan.
你的数据库已经有这个表了,把它放下,让 laravel 由 artisan 创建。
回答by Kayo Hamid
Do you have a users migration right? If so, insert your user create table inside of:
你有用户迁移权利吗?如果是这样,请插入您的用户创建表:
if(!Schema::hasTable('users')) ...
回答by Govinda Poddar
Sometimes if you are using MAMP server you might get this error because of database connectivity. This is because of the port number, usually while using MAMP the port number of mysql database is 8889 which you have to change in your configuration and run your artisan command it will work for sure.
有时,如果您使用 MAMP 服务器,您可能会因为数据库连接而收到此错误。这是因为端口号,通常在使用 MAMP 时,mysql 数据库的端口号是 8889,您必须在配置中更改它并运行您的 artisan 命令,它肯定会起作用。
回答by Tgold brain
why bother?
何苦?
you can use php artisan migrate:fresh
this will ignore the current user table and recreate the tables from fresh.
您可以使用php artisan migrate:fresh
这将忽略当前用户表并重新创建新表。
回答by Elsabe Gerhard Crous
This worked for me: delete all tables from database and run
这对我有用:从数据库中删除所有表并运行
php artisan migrate.