【问题标题】:Errors when refreshing migration of DB tables刷新数据库表迁移时的错误
【发布时间】:2017-06-27 22:38:12
【问题描述】:

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

错误

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

【问题讨论】:

  • 您如何检查迁移运行的顺序。运行php artisan migrate:rollback 将从您migrations 表的底部开始,然后继续运行

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


【解决方案1】:

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

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

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

希望这会有所帮助:)。

【讨论】:

    【解决方案2】:

    我认为你应该试试这个:

    首先从数据库的迁移表中删除 account_typesusers

    并刷新您的迁移。

    希望这对你有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 2015-06-08
      • 2015-04-06
      • 1970-01-01
      • 2017-02-21
      相关资源
      最近更新 更多