Laravel 5.6迁移

时间:2018-08-08 13:09:18

标签: php laravel

我在Laravel 5.6中的迁移存在问题。

这是一个问题:

我在Laravel中的代码:

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

class CreateCompaniesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->timestamps();
        });
    }

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

我需要做什么?

3 个答案:

答案 0 :(得分:2)

该表在消息中告诉您,该表已经存在,您可能需要先删除它,或者检查您在ENV文件中使用的数据库名称是否正确。还要检查您在上一次迁移中没有创建表公司。

如果您想将字段添加到现有表中,则不需要 create 方法,但是:

Schema::table("companies", function (Blueprint $table) {
        // The fields you need
});

对于外键,使用unsignedInteger作为数据类型也更安全

答案 1 :(得分:0)

如错误消息中所示,“ companies”表已存在。 您可以使用以下命令来解决它。

php artisan migration:rollback

php artisan migration:reset

请参阅laravel文档以获取更多详细信息。

答案 2 :(得分:0)

您可以使用以下命令删除每个表并进行迁移,然后再备份sql,因为它会删除所有数据

 php artisan migrate:fresh
相关问题