我正在使用Codeigniter 3、Ion Auth和Bootstrap 4开发社交网络应用程序。您可以在此处查看Github回购。
我已经启用了迁移,然后创建了迁移文件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的迁移。
我做错了什么?
***更新***
首先,编辑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,则可以减少迁移表中的版本值,然后重新运行迁移(如果不是自动迁移(。