Skip to content

in magento2 add unique key constaint on column which has already foreign key adding for vendor code

i trying to add unique key constraint to company_id column in company_credit table but i facing issue like duplicate key Syntax error or access violation: 1061 Duplicate key name ‘COMPANY_CREDIT_COMPANY_ID’, query was: ALTER TABLE company_credit ADD CONSTRAINT COMPANY_CREDIT_COMPANY_ID UNIQUE KEY (company_id), COMMENT=’Company Credit’

so basically this is my code in dbschema.xml

<table name="company_credit" resource="default" engine="innodb" comment="Company Credit">
<!-- Other table definitions here -->
    <constraint xsi:type="unique" referenceId="COMPANY_CREDIT_UNIQUE_COMPANY_ID">
        <column name="company_id"/>
    </constraint>
</table>

so using this code in db_scheme_whitelist.json
this code is coming

"company_credit": {
    "constraint": {
        "COMPANY_CREDIT_COMPANY_ID": true
    }
}

and this is vendor dbschema.xml code which is causing issue

 <constraint xsi:type="foreign" referenceId="COMPANY_CREDIT_COMPANY_ID_COMPANY_ENTITY_ID"
                table="company_credit" column="company_id" referenceTable="company" referenceColumn="entity_id"
                onDelete="CASCADE"/>
    <index referenceId="COMPANY_CREDIT_COMPANY_ID" indexType="btree">
        <column name="company_id"/>
    </index>

so now when i running upgrade command error is coming
enter image description here

db screenshot
currently it is showing mul key there