刷新数据库表的迁移时出错

时间:2017-02-09 19:02:14

标签: mysql laravel laravel-5 laravel-5.3 database-migration

刷新迁移后,我在用户迁移和帐户类型用户表之间遇到问题,无法找出问题。

错误

[Illuminate\Database\QueryException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'account_typ
  es' already exists (SQL: create table `account_types` (`id` int unsigned no
  t null auto_increment primary key, `name` varchar(50) not null, `created_at
  ` timestamp null, `updated_at` timestamp null) default character set utf8mb
  4 collate utf8mb4_unicode_ci)



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

我的迁移

帐户类型

 public function up()
    {
        Schema::create('account_types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50);
            $table->timestamps();
        });
    }


    public function down()
    {
        Schema::dropIfExists('account_types');
    }

用户

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('surname', 20);
            $table->string('email')->unique();
            $table->string('password');
            $table->string('mobilephone', 9);
            $table->rememberToken();
            $table->timestamps();
        });


    }
    public function down()
    {
        Schema::dropIfExists('users');
    }

用户和帐户类型之间的关系,我相信问题可能在这里,但仍然无法弄清楚迁移代码有什么问题。

用户和帐户类型的关系

 public function up()
    {
        Schema::table('users', function($table) {
            $table->integer('account_type_id')->unsigned();
            $table->foreign('account_type_id')
                ->references('id')->on('account_types');
        });
    }


    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign('users_account_type_id_foreign');
            $table->dropColumn('account_type_id');
        });
    }

2 个答案:

答案 0 :(得分:0)

就像我在迁移之前发生过多次错误之前一样,您收到此错误,因为迁移脚本没有机会注册上次迁移。

如果您不关心已存储的数据,请手动删除数据库中的所有表(甚至是迁移表)并重新运行。

当您希望保留表中的数据时,应检查此迁移表并查看批处理列。将每个条目放在要保留的1上,并将行和它下面的行设置为0(也许删除它们也可以)。运行php artisan migrate。使用phpmyadmin时,您可以通过查询更新这些行,例如:UPDATE migrations SET batch=0 WHERE migration LIKE "%create_users_table";

希望这会有所帮助:)。

答案 1 :(得分:0)

我想你应该试试这个:

首先从数据库中的迁移表中删除 account_types 用户

刷新您的迁移。

希望这适合你!

相关问题