Search code examples
zend-frameworksymfonydoctrine-ormprimary-keyauto-increment

In doctrine2 is it possible to have an autoincrement column that is not the primary key?


In doctrine2 I have an entity that has a primary key that's feeded from a webservice, and also has an index that should be an auto increment.

I can set manually in mysql but can't make this work in doctrine2.


Solution

  • The auto-increment limitation is related to the database you are using. in Mysql ,Mysql_autoincrement, it also depends on the engine you are using. exemple:

    For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as

    You can have a general documentation on auto_increment here

    In general the auto_increment is reserved to a numerical field that is part of the identifier index (one or more column involved).

    So this can work for mysql.

    However the auto_incriment in doctrine seems to only be alllowed for THe @Id (Primary Key)

    21.2.9. @GeneratedValue

    Specifies which strategy is used for identifier generation for an instance variable which is annotated by @Id. This annotation is optional and only has meaning when used in conjunction with @Id.

    If this annotation is not specified with @Id the NONE strategy is used as default.

    Required attributes:

    strategy: Set the name of the identifier generation strategy. Valid values are AUTO, SEQUENCE, TABLE, IDENTITY, UUID, CUSTOM and NONE. Example:

    ?php
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id = null;
    

    Doctrine_auto_increment

    You may want to find a work arround and post a ticket about it