is there any way to create 2 auto increment id in same table?When im trying to create using db_schema, error showing that only primary key id can get this auto increment.is there any other ways?


2 Answers 2


try below code

<table name="declarative_table">
        <column xsi:type="int" name="id_column1" padding="10" unsigned="true" nullable="false" comment="Entity Id"/>
        <column xsi:type="int" name="id_column2" padding="10" unsigned="true" nullable="false" comment="Entity Id"/>
        <column xsi:type="int" name="severity" padding="10" unsigned="true" nullable="false" comment="Severity code"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
        <column xsi:type="timestamp" name="time_occurred"  comment="Time of event"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id_column1"/>
            <column name="id_column2"/>

it's working fine. check the screenshot

enter image description here

  • no,its not working for me.
    – mage
    Jun 24, 2021 at 16:12
  • update your schema code in question
    – Devidas
    Jun 25, 2021 at 5:15

This is the way I added the multiple increment id in the table and it's work like charm:

add the desired column in the database you want to add in the increment :

<column xsi:type="varchar" name="id" nullable="true" length="255" comment="ID"/>

and then in the model use this code :

               $collection = $model->getCollection();
                $collection->getSelect()->order('id DESC');
                $lastItem = $collection->getFirstItem();
                $lastId = $lastItem->getId();

                // Increment the ID
                $newId = $lastId + 1;
                $model->setData('id', $newId);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.