Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Add column if not exist
 
rbncha
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2008-08-20
 

can anyone suggest me how to check if a column already exists and add if not exists only ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Raptor Commerce (Matt)
Member
 
Total Posts:  49
Joined:  2009-08-10
 
Rubin - 06 August 2009 12:27 AM

can anyone suggest me how to check if a column already exists and add if not exists only ?

Are you trying to run this from a script? You’ll need to make 2 calls ... firstly to check for the existence of the column via a ‘DESC’ or ‘SHOW CREATE TABLE’ statement, then add the column if it doens’t exist. There’s no ‘ADD COLUMN IF NOT EXISTS’ syntax

Matt

 
Magento Community Magento Community
Magento Community
Magento Community
 
diffstrat
Jr. Member
 
Total Posts:  4
Joined:  2010-07-09
 

You could use the methods provided by the Varien_Db_Adapter_Pdo_Mysql class. There’s an addColumn() method which will check if the column exists before adding it.

So for example, if you were trying to add the column in a sql resource setup file, you could do something like:

$connection $this->getConnection();
$connection->addColumn($tableName$columnName$definition)

Obviously, you’d also need to define the method argument variables;

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top