【问题标题】:Laravel Migration - table not getting createdLaravel 迁移 - 未创建表
【发布时间】:2013-09-26 13:49:54
【问题描述】:

我正在尝试学习 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 表?

EDIT 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');
    }

}

【问题讨论】:

    标签: mysql laravel laravel-4


    【解决方案1】:

    更新后的 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

    【讨论】:

    • 我无法回滚,因为如果从一开始就没有创建“用户”表,就无法删除它。我改为手动重置数据库。您的代码有效。为什么这样做?我是按照教程一步一步来的。
    • 我认为,在第一次你没有编写表创建或下降功能的代码。现在可以用了吗?
    • 原代码不是我写的。我只是按照教程中的步骤进行操作。我已经更新了我的问题并发布了原始迁移文件。为什么那行不通?为什么你的代码有效?
    • 更新的 laravel 应该使用 Blueprint 来创建迁移模式。检查更新的答案。
    • 遗憾的是,这个 API 文档并没有(无论如何以明确的方式)为什么需要它或正在发生什么。例如:为什么生成的迁移文件不自动包含该行?
    【解决方案2】:

    我遇到了同样的问题,在搜索 Laravel 文档 (link to documentation) 后,我找到了一个解决该问题的工匠命令:

    php artisan migrate:refresh
    

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

    【讨论】:

      猜你喜欢
      • 2017-07-02
      • 2019-05-17
      • 2023-01-04
      • 2015-03-28
      • 2023-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 2021-06-29
      相关资源
      最近更新 更多