I know the syntax to create table via ‘InstallSchema’ in magento2 but don’t know how to add UNIQUE Constraint on two columns of my table.
here is the syntax of my table –
$ table_mediatype = $ setup->getConnection()->newTable($ setup->getTable('mediatype')); $ table_mediatype->addColumn( 'id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, array('identity' => true,'nullable' => false,'primary' => true), 'Entity ID' ); $ table_mediatype->addColumn( 'mediadownloader_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, array('nullable' => false), 'Mediadownloader ID' ); $ table_mediatype->addColumn( 'product_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, array('nullable' => false), 'Product ID' ); $ table_mediatype->addColumn( 'media_type', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 25, array('nullable' => false), 'Associated Media Type' );
Via sql query we can add UNIQUE Contraint like this –
ALTER TABLE netzwelt_mediatype ADD CONSTRAINT unique_product_id_with_media_type UNIQUE unique_product_id_with_media_type(
product_id,
media_type );
But I don’t know how to add via ‘InstallSchema’, Can anyone help me in this ?
Thanks