Set a field as UNIQUE KEY in Magento 2 Setup Schema

Single Field :

->addIndex(
    $installer->getIdxName(
        'YOUR_TABLE_NAME',
        ['YOUR_UNIQUE_FIELD_NAME'],
        \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
    ),
    ['YOUR_UNIQUE_FIELD_NAME'],
    ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
)

 

You can also set by multiple option

->addIndex(
    $installer->getIdxName(
        'catalog_product_entity_datetime',
        ['entity_id', 'attribute_id', 'store_id'],
        \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
    ),
    ['entity_id', 'attribute_id', 'store_id'],
    ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
)

 

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.