Magento – Upgrade script not working in magento custom module with product relationship

upgrade-script

I have created one install script install-1.0.0.0.1.php that is as below

$installer = $this;

$table = $installer->getConnection()
->newTable($installer->getTable('magentostudy_news/news'))

->addColumn('news_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
    'unsigned' => true,
    'identity' => true,
    'nullable' => false,
    'primary'  => true,
), 'Entity id')

->addColumn('title', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
    'nullable' => true,
), 'Title')

->addColumn('author', Varien_Db_Ddl_Table::TYPE_TEXT, 63, array(
    'nullable' => true,
    'default'  => null,
), 'Author')
->addColumn('content', Varien_Db_Ddl_Table::TYPE_TEXT, '2M', array(
    'nullable' => true,
    'default'  => null,
), 'Content')
->addColumn('image', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
    'nullable' => true,
    'default'  => null,
), 'News image media path')
->addColumn('published_at', Varien_Db_Ddl_Table::TYPE_DATE, null, array(
    'nullable' => true,
    'default'  => null,
), 'World publish date')
->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
    'nullable' => true,
    'default'  => null,
), 'Creation Time')
->addIndex($installer->getIdxName(
        $installer->getTable('magentostudy_news/news'),
        array('published_at'),
        Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX
    ),
    array('published_at'),
    array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX)
)
->setComment('News item');

$installer->getConnection()->createTable($table);

and here is my upgrade script mysql4-upgrade-1.0.0.0.1-1.0.0.0.2.php for alter existing table to linking product with my table

  $installer = $this;

  $table = $installer->getConnection()

->addColumn($installer->getTable('magentostudy_news/news'),'product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
    'unsigned'  => true,
    'nullable'  => false,
    'default'   => '0',
), 'Product ID')

->addColumn($installer->getTable('magentostudy_news/news'),'position', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
    'nullable'  => false,
    'default'   => '0',
), 'Position')

->addIndex($this->getIdxName('magentostudy_news/news', array('product_id')), array('product_id'))

->addForeignKey($this->getFkName('magentostudy_news/news', 'news_id', 'magentostudy_news/news', 'entity_id'), 'news_id', $this->getTable('magentostudy_news/news'), 'entity_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)

->addForeignKey($this->getFkName('magentostudy_news/news', 'product_id', 'catalog/product', 'entity_id'),    'product_id', $this->getTable('catalog/product'), 'entity_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
->setComment('News to Product Linkage Table');

When i run my URL its simply displaying blank page with no error messages There is some problem with upgrade script syntax only as install script working fine and when i remove upgrade script whole site working fine…plz help

Best Answer

I just had this problem. For me, the solution was to unchain the addColumn calls in your upgrade script. Like this:

$installer->getConnection()
    ->addColumn($installer->getTable('magentostudy_news/news'),'product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'unsigned'  => true,
        'nullable'  => false,
        'default'   => '0',
    ), 'Product ID');

$installer->getConnection()
    ->addColumn($installer->getTable('magentostudy_news/news'),'position', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'nullable'  => false,
        'default'   => '0',
    ), 'Position');

The addColumn in this case returns the query results rather than the connection so they cannot be chained.

Try deleting your tables and simplifying your upgrade script. Add one field at a time so you can find out which column is causing the error.

Also, the error should be somewhere. Make sure you enable logging in the admin and check the exception.log.

Related Topic