【问题标题】:SQLite does not create an indexSQLite 不创建索引
【发布时间】:2018-02-21 05:01:53
【问题描述】:

我正在 Laravel 5.4 中进行一些数据库迁移。迁移与 MySQL 数据库一起工作正常,但为了测试我想使用 SQLite 但迁移失败。这是代码

public function up()
    {
        Schema::create('mapped_venues', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('upload_job_id')->nullable();
            $table->string('venue')->default('');
            $table->unsignedInteger('venue_id')->nullable();

            $table->timestamps();

            $table->index(['venue']);
        });

        Schema::create('mapped_teams', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('upload_job_id')->nullable();
            $table->string('team')->default('');
            $table->unsignedInteger('team_id')->nullable();

            $table->timestamps();

            $table->index(['team']);
        });
    }

当我运行php artisan migrate 时,mapped_teams.team 列上的索引没有创建,但mapped_venues.venue 上的索引是!!

$ sqlite3 database/database.sqlite 
SQLite version 3.19.3 2017-06-08 14:26:16
Enter ".help" for usage hints.
sqlite> .indexes mapped_teams
sqlite> .indexes mapped_venues
mapped_venues_venue_index
sqlite>

我也尝试在单独的调用中创建索引

Schema::table('mapped_venues', function (Blueprint $table) {
            $table->index(['venue']);
        });
Schema::table('mapped_teams', function (Blueprint $table) {
            $table->index(['team']);
        });

但结果是一样的。有趣的是,当我(错误地)将索引$table->index['team']) 的创建留在创建表的调用中时(因此,我有两个创建索引的调用)我得到索引mapped_teams_team_index 已经存在的错误。

我正在使用:

  • Laravel 5.4.36
  • Doctrine DBal 2.6.2
  • SQLite 3.19.3

【问题讨论】:

    标签: php laravel sqlite database-migration


    【解决方案1】:

    我实际上发现该索引确实已创建。我有另一个迁移将team 列重命名为mapped_team,我想先删除索引

    Schema::table('mapped_teams', function (Blueprint $table) {
        $table->dropIndex(['team']);
        $table->renameColumn('team', 'mapped_team');
        $table->index(['mapped_team']);
    }
    

    删除索引的行抱怨索引mapped_teams_team_index 不存在。我已将迁移修改为不删除并重新创建索引,而只是重命名它。结果是名为mapped_teams_team_index 的索引仍然存在,但它现在正确地索引mapped_team 列。这适用于 Mysql 和 SQLite。

    【讨论】:

      【解决方案2】:

      您在调用索引函数时似乎犯了一个小错误(将其用作数组而不是函数):

          $table->index['venue'];
      

      应该是:

          $table->index('venue');
      

      【讨论】:

      • 谢谢@inet123,这只是问题的输入错误。实际代码很好。我已经编辑了这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2016-09-17
      • 1970-01-01
      • 2011-10-16
      • 2017-12-03
      • 2012-09-09
      • 1970-01-01
      相关资源
      最近更新 更多