Magento 1.9 – Custom Module Table Not Found, Install Script Not Executed

databaseinstall-scriptmagento-1.9modulesql

I am learning magento, trying to add a custom module where one can upload .xls files and then manage them(basic CRUD operations). Then the .xls files can be executed and inserted into the products db.

This is the error I get in the magento dashboard

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.wine' doesn't exist, query was: SELECT COUNT(*) FROM `wine` AS `main_table

As far as I understand, the table wasn't found, which means that my install wasn't ran. This is my Mymodule/Wine/sql/wine_setup/install-0.1.0.php

<?php

$installer = $this;

$installer->startSetup();

$table = $installer->getConnection()
    ->newTable($installer->getTable('wine'))
    ->addColumn('wine_id', Varien_Db_Ddl_Table::TYPE_INTEGER, 10, array(
            'auto_increment' => true,
            'unsigned' => true,
            'nullable' => false,
            'primary' => true
        ), 'Wine Id')
    ->addColumn('name', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
            'nullable' => false,
        ), 'Name')
    ->addColumn('status', Varien_Db_Ddl_Table::TYPE_INTEGER, 1, array(
            'nullable' => false,
        ), 'Status')
    ->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_DATETIME, null, array(
            'nullable' => false,
        ), 'Created At')
    ->addColumn('executed_at', Varien_Db_Ddl_Table::TYPE_DATETIME, null, array(
            'nullable' => false,
        )
    )
    ->setOption('type', 'InnoDB')
    ->setOption('charset', 'utf8')
    ->setComment('Wine .xls Files Table');
$installer->getConnection()->createTable($table);

$installer->endSetup();

The config.xml part related to Models/Resources:

<models>
    <wine>
        <class>Mymodule_Wine_Model</class>
        <resourceModel>wine_mysql4</resourceModel>
    </wine>
    <wine_mysql4>
        <class>Mymodule_Wine_Model_Resource</class>
        <entities>
            <wine>
                <table>wine</table>
            </wine>
        </entities>
    </wine_mysql4>
</models>
<resources>
    <wine_setup>
        <setup>
            <module>Mymodule_Wine</module>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </wine_setup>
    <wine_write>
        <connection>
            <use>core_connection</use>
        </connection>
    </wine_write>
    <wine_read>
        <connection>
            <use>core_read</use>
        </connection>
    </wine_read>
</resources>

My model located in Mymodule/Wine/Model/Wine.php

<?php

class Mymodule_Wine_Model_Wine extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('wine/wine');
    }
}

Mymodule/Wine/Model/Resource/Wine.php

<?php

class Mymodule_Wine_Model_Resource_Wine extends Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
        $this->_init('wine/wine', 'wine_id');
    }
}

Mymodule/Wine/Model/Resource/Wine/Collection.php

<?php

class Mymodule_Wine_Model_Resource_Wine_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct()
    {
        // parent::_construct();
        $this->_init('wine/wine');
    }
}

The error only occurs only if this line is present:

<?php

class Mymodule_Wine_Adminhtml_WineController extends Mage_Adminhtml_Controller_Action
{
    protected function _initAction()
    {
        $this->loadLayout()
              ->_setActiveMenu('wine/items')
              ->_addBreadcrumb(Mage::helper('adminhtml')->__('Manage .xls Files'), Mage::helper('adminhtml')->__('Manage .xls Files'));
        return $this;
    }

    public function indexAction()
    {
        $this->_initAction();
        >> $this->_addContent($this->getLayout()->createBlock('wine/adminhtml_wine')); <<== without this line I can access the backend, but i need the block to be inserted in the dashboard
        $this->renderLayout();
    }
}

Best Answer

check the module version in config.xml and the check module entry in core_resource table if it is there remove it.

<modules>
        <Spacename_Modulename>
            <version>0.1.0</version>
        </Spacename_Modulename>
</modules>