Laravel迁移 - 表格未被创建

3
我正在学习Laravel。我正在遵循快速入门文档,但在迁移方面遇到了问题。我已经到了这一步:http://laravel.com/docs/quick#creating-a-migration 当我运行命令php artisan migrate时,命令行显示如下:
c:\wamp\www\laravel>php artisan migrate
Migration table created successfully.
Migrated: 2013_09_21_040037_create_users_table

在数据库中,我看到创建了一个名为migrations的表,并且有1条记录。然而,我没有看到一个名为users的表。因此,我无法继续进行ORM部分的教程。
有什么想法吗?为什么users表没有被创建? 编辑1(原始迁移文件):
<?php

use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
            Schema::create('users', function($table)
            {
                $table->increments('id');
                $table->string('email')->unique();
                $table->string('name');
                $table->timestamps();
            });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
            Schema::drop('users');
    }

}
2个回答

8
更新后的Laravel应该使用Blueprint创建数据库架构。因此,请尝试将您的用户迁移文件内容更改为以下内容:
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->integer('id', true);
            $table->string('name');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}

然后运行。
  php artisan migrate:rollback 

然后再次迁移。

在此处阅读 API 文档 http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html


1
我无法回滚,因为如果“users”表从未被创建,它就无法被删除。我手动重置了数据库。你的代码有效。为什么?我一步一步地按照教程操作的。 - StackOverflowNewbie
我认为,第一次你没有在向上和向下函数中编写表创建或删除的代码。现在它是否正常工作? - Anshad Vattapoyil
1
我没有编写原始代码。我只是按照教程中的步骤进行操作。我已更新我的问题并发布了原始的迁移文件。为什么那不起作用?为什么你的代码可以工作? - StackOverflowNewbie
更新的 Laravel 应该使用 Blueprint 来创建迁移架构。请检查更新后的答案。 - Anshad Vattapoyil
1
遗憾的是,这个API文档并没有清楚地解释为什么需要它或者发生了什么。例如:为什么生成的迁移文件不会自动包含那一行呢? - Trass Vasston

0

我遇到了同样的问题,在查阅 Laravel 文档 (文档链接) 后,我找到了一个 Artisan 命令来解决它:

php artisan migrate:refresh

migrate:refresh 命令将回滚所有迁移,然后执行 migrate 命令。该命令有效地重新创建整个数据库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接