Search code examples
phpmysqlauto-incrementdatabase-migrationphalcon

auto_increment in Phalcon migrations system


For some reason, the migration system is not behaving well with auto_increment primary keys. Am I overseeing something? (I'm using phalcon 2.0.1)

I have the following auto generated migration:

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'
        )
    )
    );
}
}

But when I run that migration, the generated sql does:

==> 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  

As you can see, the id field is modified not including an auto_increment.


Solution

  • I'm answering my own question since it seems to be a bug in the Phalcon migrations system.

    As @axalix points out, the migration is done on a table the previously existed. (I upvoted your answer because it helped me to get to the solution).

    This is the original table:

    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
    

    And this is the modified table (modified by 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
    

    It turns out that I forgot the "unsigned" option in the Doctrine entity, so when the tables was changed, the table looked like

    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
    

    For some reason, running the phalcon migration without the unsigned option made it to forget about the auto_increment.

    I'll try to reproduce it in isolation to report the bug to the Phalcon guys.