【问题标题】:auto_increment in Phalcon migrations systemPhalcon 迁移系统中的 auto_increment
【发布时间】:2015-08-01 19:03:28
【问题描述】:

由于某种原因,迁移系统在使用 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。

【问题讨论】:

  • 有趣。只是为了确认一下,您是否有这个还建议安装 PHP 5.4 或更高版本。

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


【解决方案1】:

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

不仅如此,您不是添加新列id,而是更改它MODIFY id INT(11) NOT NULL。那么之前有什么? varchars,非唯一值?如果是这样,添加autoincrement 标志是不可能的。

【讨论】:

  • 桌子在那里(我正在将遗留项目过渡到 Phalcon)。对表的唯一更改是活动列(我添加了它)。之前,列 id 是 INT(11) NOT NULL AUTO_INCREMENT
  • 据我了解,迁移的目的是“迁移”,即应用列出的更改(而不是描述您已经拥有的内容)。如果您已经有一个标记为 NOT NULL AUTO_INCREMENT 的列,则无需使用 'autoIncrement' => true' 添加新的 Column 块。
  • 另一方面,如果您只想出于一致性目的进行所有迁移,那很好,但不要期望在日志中看到AUTO_INCREMENT,因为它已经存在。如果你想确保你的迁移能在空数据库上正常工作,只需在空数据库上尝试一下,你就会发现它可以工作(我已经为你测试过了)。
  • 好吧,我想做的只是拥有新字段,我使用了命令:phalcon migration generate --table=languages。你的意思是我不应该使用自动生成,而是手动编码迁移?
  • 您可以使用自动生成来根据您已有的表(1.0.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

这是修改后的表(通过学说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实体中的“无符号”选项,所以当表格被改变时,表格看起来像

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

由于某种原因,运行 phalcon 迁移而不使用 unsigned 选项使其忘记了 auto_increment。

我将尝试单独复制它,以便向 Phalcon 人员报告该错误。

【讨论】:

    猜你喜欢
    • 2017-11-19
    • 2014-09-27
    • 2012-12-21
    • 2016-04-15
    • 2012-02-24
    • 2022-08-18
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多