Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 626
jmarcone Avatar asked Feb 19 '13 18:02

jmarcone


1 Answers

I used columnDefinition of INT AUTO_INCREMENT UNIQUE

/**
 * @var integer
 *
 * @ORM\Column(type="integer", name="sequence", nullable=true, columnDefinition="INT AUTO_INCREMENT UNIQUE")
 */
protected $sequence = null;

Doctrine migration bundle generates

$this->addSql('ALTER TABLE table_name_here ADD sequence INT AUTO_INCREMENT UNIQUE');

Edit: Note that this answer is from 2016, it may no longer be valid. See accepted answer.

like image 191
gingerCodeNinja Avatar answered Oct 14 '22 09:10

gingerCodeNinja