【问题标题】:What causes this "No migration could be found with the version number" error in Codeigniter 3?是什么导致 Codeigniter 3 中出现“无法使用版本号找到迁移”错误?
【发布时间】:2020-09-28 11:24:57
【问题描述】:

我正在使用 Codeigniter 3、Ion-Auth 和 Bootstrap 4 开发一个社交网络应用程序。您可以看到 Github repo HERE .

我启用了迁移,然后创建了迁移文件002_add_messages_table.php

class Migration_Create_Messages_Table extends CI_Migration {

  public function up()
  {
    $this->dbforge->add_field(array(
      'id'=>array(
        'type'=>'INT', 
        'constraint' => 11,
        'unsigned' => TRUE,
        'auto_increment' => TRUE
      ),

      'from'=>array(
        'type'=>'INT',
        'constraint' => 11,
        'null'       => FALSE
      ),

      'to'=>array(
        'type'=>'INT',
        'constraint' => 11,
        'null'       => FALSE
      ),

      'message'=>array(
        'type'=>'TEXT',
      ),

      'status'=>array(
        'type'=>'TINYINT',
        'constraint' => 1,
      ),

      'created_at'=>array(
        'type'=>'TIMESTAMP',
        'default' => NULL
      ),

    ));
    
    $this->dbforge->add_key('id', TRUE);
    $this->dbforge->create_table('messages');
  }

    public function down() {
        $this->dbforge->drop_table('messages');
    }

}

我还有一个Migrate.php 控制器,里面有这个内容:

<?php defined("BASEPATH") or exit("No direct script access allowed");

class Migrate extends CI_Controller{

    public function index($version){
        $this->load->library("migration");

      if(!$this->migration->version($version)){
          show_error($this->migration->error_string());
      } else {
        echo "Tables created";
      }  
    }
}

当我访问http://myapp.com/index.php/migrate/index/002 时,应用程序没有创建messages 表,而是抛出以下消息:

找不到版本号为 002 的迁移。

我做错了什么?

【问题讨论】:

  • @Vickel users 表中已经存在的记录会发生什么?
  • @Vickel 另外,我应该访问什么 URL?
  • @RazvanZamfir 我试过打破它,但我无法复制你的错误。它对我有用。我得到迁移 1 和 2 的工作并且迁移表得到更新。
  • @RazvanZamfir 关于您的 users 表,仅当您运行 001 迁移表时,您的组、用户、users_groups 和 login_attempts 表将被删除(它在您使用的迁移文件代码中)。否则他们会没事的。

标签: php codeigniter


【解决方案1】:

*** 已更新 ***

首先,编辑 application/config/migration.php 并设置

$config['migration_type'] = 'sequential'; // was 'timestamp' in your repo

其次,编辑您的迁移文件并将类名更改为

class Migration_Add_Messages_Table extends CI_Migration {

或将文件重命名为 002_create_messages_table (类名和文件名要相似)

那么 如果出现以下查询

SELECT version FROM migrations

返回大于或等于 2 的任何值,您可以做的是减少迁移表中的版本值,然后重新运行迁移(如果它不是自动的)。

【讨论】:

  • 我的 migrations 表中没有记录。
  • 如果你在 1 上运行了这个,你应该在你的迁移表中有一个条目。事实上你会的。
  • @TimBrownlaw 我应该访问哪个 URL?
  • 我收到这条消息,告诉我一些表已经存在。我知道,我只想添加其他表。如何跳过现有表?
  • 您必须为此提出一个新问题。当我复制克隆您的存储库的解决方案时,我已经正确回答了您的原始问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 2020-12-13
相关资源
最近更新 更多