Phalcon迁移系统中的auto_increment

时间:2015-05-20 13:33:30

标签: php mysql auto-increment database-migration phalcon

出于某种原因,迁移系统在auto_increment主键上表现不佳。我在监督什么吗? (我正在使用phalcon 2.0.1)

我有以下自动生成的迁移:

class LanguagesMigration_100 extends Migration
{

public function up()
{
    $this->morphTable(
        'languages',
        array(
        'columns' => array(
            new Column(
                'id',
                array(
                    'type' => Column::TYPE_INTEGER,
                    'notNull' => true,
                    'autoIncrement' => true,
                    'size' => 11,
                    'first' => true
                )
            ),
            new Column(
                'ccode',
                array(
                    'type' => Column::TYPE_VARCHAR,
                    'notNull' => true,
                    'size' => 6,
                    'after' => 'id'
                )
            ),
            new Column(
                'active',
                array(
                    'type' => Column::TYPE_INTEGER,
                    'notNull' => true,
                    'size' => 1,
                    'after' => 'ccode'
                )
            )
        ),
        'indexes' => array(
            new Index('PRIMARY', array('id')),
            new Index('UNIQ_A0D153794EE11504', array('ccode'))
        ),
        'options' => array(
            'TABLE_TYPE' => 'BASE TABLE',
            'AUTO_INCREMENT' => '9',
            'ENGINE' => 'InnoDB',
            'TABLE_COLLATION' => 'utf8_unicode_ci'
        )
    )
    );
}
}

但是当我运行迁移时,生成的sql会执行:

==> default: Phalcon DevTools (2.0.1)
==> default: 1432127144.0475
==> default: : 
==> default: SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`= 'languages' AND `TABLE_SCHEMA` = 'euromillions'
==> default:   => 
==> default: 1432127144.0619
==> default:  (
==> default: 0.014361143112183
==> default: )
==> default: 1432127144.0767
==> default: : 
==> default: DESCRIBE `euromillions`.`languages`
==> default:   => 
==> default: 1432127144.1014
==> default:  (
==> default: 0.024682998657227
==> default: )
==> default: 1432127144.1033
==> default: : 
==> default: ALTER TABLE `languages` MODIFY `id` INT(11) NOT NULL
==> default:   => 1432127144.1488 (0.045513153076172)
==> default: 1432127144.1489: ALTER TABLE `languages` ADD `active` INT(1) NOT NULL AFTER ccode
==> default:   => 1432127144.17 (0.021080017089844)
==> default: 1432127144.1701: SHOW INDEXES FROM `euromillions`.`languages`
==> default:   => 1432127144.1715 (0.001410961151123)
==> default: 1432127144.1717: ALTER TABLE `languages` ADD INDEX `UNIQ_A0D153794EE11504` (`ccode`)
==> default:   => 1432127144.181 (0.0093460083007812)
==> default: 1432127144.1811: ALTER TABLE `languages` DROP INDEX `ccode`
==> default:   => 1432127144.185 (0.0038588047027588)
==> default: 1432127144.185: ALTER TABLE `languages` DROP INDEX `ccode_2`
==> default:   => 1432127144.1883 (0.0032830238342285)
==> default:                                                     
==> default:   Success: Version 1.0.0 was successfully migrated  

如您所见,修改了id字段,但不包括auto_increment。

2 个答案:

答案 0 :(得分:2)

我正在回答我自己的问题,因为它似乎是Phalcon迁移系统中的一个错误。

正如@axalix指出的那样,迁移是在以前存在的表上完成的。 (我赞成你的答案,因为它帮助我找到解决方案)。

这是原始表:

CREATE TABLE `languages` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `ccode` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ccode` (`ccode`),
  KEY `ccode_2` (`ccode`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

这是修改后的表(由doctrine orm:schema-tool修改)

CREATE TABLE `languages` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `ccode` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
  `active` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQ_A0D153794EE11504` (`ccode`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

事实证明我忘记了Doctrine实体中的“unsigned”选项,因此当表格被更改时,表格看起来像

CREATE TABLE `languages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ccode` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
  `active` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQ_A0D153794EE11504` (`ccode`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

出于某种原因,在没有unsigned选项的情况下运行phalcon迁移使它忘记了auto_increment。

我会尝试单独复制它以向Phalcon家伙报告错误。

答案 1 :(得分:1)

好吧,当我尝试运行迁移时,表是使用auto_increment创建的。但是我从你的日志中看到表正在被修改(没有创建),这意味着你已经有了一个先前创建的表。

除此之外,您不是要添加新列id,而是更改MODIFY id INT(11) NOT NULL。那之前有什么? varchars ,非唯一值?如果是这样,则无法添加autoincrement标志。