I am having custom table called channel with channel_id,channel_code,channel_name and so on. I want to make channel_code and channel_name unique by using installscript or upgradescript here is my code:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of InstallSchema
*
* @author Pramod Kharade
*/
namespace Blazeclan\Channels\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class InstallSchema implements InstallSchemaInterface {
//put your code here
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) {
$installer = $setup;
$installer->startSetup();
// Get tutorial_simplenews table
$tableName = $installer->getTable('blazeclan_channels');
// Check if the table already exists
if ($installer->getConnection()->isTableExists($tableName) != true) {
// Create tutorial_simplenews table
$table = $installer->getConnection()
->newTable($tableName)
->addColumn(
'channel_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'Channel ID'
)
->addColumn(
'channel_code',
Table::TYPE_TEXT,
null,
['nullable' => false,'DEFAULT' => null],
'Channel Code'
)
->addColumn(
'channel_name',
Table::TYPE_TEXT,
null,
['nullable' => false,'DEFAULT' => null],
'Channel Name'
)
->addColumn(
'channel_image',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
[],
'Channel Image'
)
->addColumn(
'channel_createdby',
Table::TYPE_TEXT,
null,
['nullable' => false, 'default' => ''],
'Channel Created By'
)
->addColumn(
'channel_modifiedby',
Table::TYPE_TEXT,
null,
['nullable' => false, 'default' => ''],
'Channel Modified By'
)
->addColumn(
'created_at',
Table::TYPE_DATETIME,
null,
['nullable' => false],
'Created At'
)
->addColumn(
'updated_at',
Table::TYPE_DATETIME,
null,
['nullable' => false,'default' => Table::TIMESTAMP_INIT_UPDATE],
'updated_at At'
)
->addColumn(
'status',
Table::TYPE_SMALLINT,
null,
['nullable' => false, 'default' => '0'],
'Status'
)
->setComment('channels Table')
->setOption('type', 'InnoDB')
->setOption('charset', 'utf8');
$installer->getConnection()->createTable($table);
}
$installer->endSetup();
}
}
Thanks In advance!