外键错误,基表或视图已经存在' laravel5

时间:2017-09-19 17:43:57

标签: php laravel laravel-5

首先,我已经找到了这个错误,并没有提供解决方案。我已将临床表的clinic_id foranea密钥添加到我的用户表中,但在进行迁移时,我得到以下错误:

  [Illuminate\Database\QueryException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table `users` (`id` int unsigned not null auto_increment primary key, `name` varc
  har(150) not null, `surnames` varchar(150) not null, `email` varchar(150) not null, `password` varchar(150) not null, `phone` int not null, `clinic_id` int not null, `remember_toke
  n` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)


  [PDOException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
  

2014_10_12_000000_create_clinics_table.php:

    public function up()
{

    Schema::create('clinics', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('country');
        $table->string('province');
        $table->timestamps();
    });
}
  

2014_10_12_000001_create_users_table.php:

  public function up()
{


    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('surnames');
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('phone');
        $table->integer('clinic_id');
        $table->foreign('clinic_id')->references('id')->on('clinics');
        $table->rememberToken();
        $table->timestamps();
    });
}

我回到了我的项目的上一个版本,我得到的错误没有再出现,现在显然它使一切正确,但仍然没有将外部字段添加到表的用户,我尝试这样:

    public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('surnames');
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('clinic_id');
        $table->foreign('clinic_id')->references('id')->on('clinics');
        $table->rememberToken();
        $table->timestamps();
    });

}

并且喜欢这个;

   public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('surnames');
            $table->string('email')->unique();
            $table->string('password');

            $table->rememberToken();
            $table->timestamps();
        });
        Schema::table('users', function (Blueprint $table) {
        $table->integer('clinic_id');
        $table->foreign('clinic_id')->references('id')->on('clinics');
    });
    }

3 个答案:

答案 0 :(得分:1)

您想要添加到现有表,而不是创建新表。因此,您只需要列出您要添加的列,并使用table代替create

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->integer('clinic_id');
        $table->foreign('clinic_id')->references('id')->on('clinics');
    });
}

答案 1 :(得分:0)

消息似乎非常清楚,您正在尝试创建一个已存在的表,尝试运行:

php artisan migrate:rollback

如果这不起作用它可能是你回滚但是错过了放下桌子,手动放下它。如果您尝试添加列错误,则需要调用现有表并附加新列。

Schema::table('users', function (Blueprint $table) {
        $table->integer('clinic_id');
        ...
    });

编辑: 如果您使用php artisan make:auth来创建auth系统,那么您已经进行了users表迁移,如果您这样做了,那么尝试创建名为users的新表是个问题,因为你使用了以下内容:

Schema::create('users', function (Blueprint $table) {
        });//This is just for creating a new users table

如果要在用户表中创建新列,则需要进行以下迁移:

Schema::table('users', function (Blueprint $table) {
    });// See the difference?, create/table

如果您不想让自己感到复杂,只需删除刚刚创建的用户模型的迁移,并将列添加到原始users迁移中。

答案 2 :(得分:0)

您忘记将unsigned()添加到integer('clinic_id')

    $table->integer('clinic_id')->unsigned();
    $table->foreign('clinic_id')->references('id')->on('clinics');

手动删除所有表并运行php artisan migrate,并进行上述更改。