无法创建新迁移

时间:2017-03-27 08:33:37

标签: laravel-5

我想创建一个新表users_profile 这是迁移代码

<?php

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

class CreateUsersProfileTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users_profile', function (Blueprint $table) {
            $table->increments('id');
            $table->string('city');
            $table->string('country');
             $table->integer('users_id')->unsigned();
                $table->foreign('users_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');               
            $table->timestamps();
        });
    }

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

面临错误

  

基表或视图已存在:1050表“users”已存在

这是用户的迁移代码

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void

     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

虽然我正在创建一个新的迁移,但是我只在user_profiles中将user表的主键声明为外键但面临上述错误 请帮助!

2 个答案:

答案 0 :(得分:0)

只需提出建议,使用Laravel Nomenclature来避免这些问题。始终将您的表名定义为多个snakecase并使用单一的camelcase模型来访问它们。

在您的情况下,请将您的表名更改为:user_profiles并使用UserProfile模型自动访问它。

您需要相应地更改迁移中的班级名称: CreateUserProfilesTable

我认为它应该可以解决您的问题。

我相信您正在使用:php artisan migrate并且由于用户表已经存在,再次为users表运行migration会产生该错误。

使用:composer dump-autoload然后php artisan migrate:refresh 回滚所有迁移并重新安装迁移。

答案 1 :(得分:0)

简单地转到数据库并删除架构表(“用户”),然后删除架构表中的条目(“迁移”)并再次运行迁移...您的问题已解决....