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?

16

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'
    ));  
}
  • Yes it is to check column of table. Have you tried this ? – Priyank May 26 '17 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.