Codeigniter/Migration不起作用
问题描述:
我的迁移不起作用。我在浏览器中收到“迁移工作”,但没有创建我所期望的博客表格。Codeigniter/Migration不起作用
这将检查migration.php中的版本号,然后查找 以migrations文件夹中该数字(例如001_)开头的文件。
<?php
// this is controller/Migration.php
class Migration extends CI_Controller {
function index() {
$this->load->library('migration');
if (! $this->migration->current()) {
show_error($this->migration->error_string());
} else {
echo "Migration Worked";
}
}
}
这是002_install_blog.php它创建博客表。
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Install_blog extends CI_Migration {
public function up()
{
// Drop table 'blog' if it exists
$this->dbforge->drop_table('blog', TRUE);
// Table structure for table 'blog'
$this->dbforge->add_field(array(
'id' => array(
'type' => 'MEDIUMINT',
'constraint' => '8',
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'slug' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'body' => array(
'type' => 'TEXT',
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('blog');
}
public function down()
{
$this->dbforge->drop_table('blog', TRUE);
}
}
这是migration.php。它拥有迁移版本。它被设置为2以便使用002_install_blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['migration_enabled'] = TRUE;
$config['migration_type'] = 'sequential';
$config['migration_table'] = 'migrations';
$config['migration_auto_latest'] = TRUE;
$config['migration_version'] = '2';
$config['migration_path'] = APPPATH.'migrations/';
答
我想通了。我有一张名为“迁移”的表格。我错误地将我的表放入mysql中,并没有运行迁移文件。然后,我运行了迁移文件,但是,“迁移”表中的版本号仍然设置为2.我手动将版本号设置为1,然后运行迁移文件。在运行它后,看看版本号在迁移表中,并确定版本为2.
道德故事。无需手动删除表格。它应该全部在迁移文件中处理!
https://github.com/AimalAzmi/codeigniter-igigrations 试试这个,我已经为此写了一个库,可以很容易地通过CLI使用它。它可以用来创建迁移文件并向后或向前运行迁移。 –