8

I want to prevent error that column in database table exits.

How can I add column to table only if column does not exist?

1 Answer 1

19

You can use this to check if table column exists.

$installer = $this;
$installer->startSetup();

$connection = $installer->getConnection();

$tableName = $installer->getTable('your_table_name');
$columnName = 'testcolumn';

if ($connection->tableColumnExists($tableName, $columnName) === false) {
    $connection->addColumn($tableName, $columnName, array(
        'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
        'nullable'  => false,
        'length'    => 255,
        'after'     => null, // column name to insert new column after
        'comment'   => 'Title'
    ));  
}
1
  • Yes it is to check column of table. Have you tried this ?
    – Priyank
    Commented May 26, 2017 at 15:43

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.