Database Error – Base Table or View Not Found: 1146 in Magento 1.9

databaseerrormagento-1.9

I am having an issue where I continually get the error:

Base table or view not found: 1146

I have created a script in the SQL directory of the module. Here is the script.

<?php

$installer = $this;

$installer = $startSetup();

$table = $installer->getConnection()
    ->newTable($installer->getTable('hwcustomer/messages'))
    ->addColumn('message_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Id')
    ->addColumn('subject', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable'  => false,
        ), 'Subject')
    ->addColumn('message', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable'  => false,
        ), 'Message')
    ->addColumn('reply', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable'  => false,
        ), 'Reply')
    ->addColumn('status', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable'  => false,
        ), 'Status')
    ->addColumn('original_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null,array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Id');

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

$installer->endSetup();

?>

I have set the version of the module back to the previous version in the core_resource table so when I refresh the page, it will create this table. But it still get this error after the version number is updated in the core_resource table.

Why am I getting this error?

Best Answer

The issue is likely because you don't have a mapping of the resource name to the table name. Your getTable statement is attempting to access the table defined under hwcustomer/messages.

Ideally in your etc/config.xml file for your module you would have a config node that defines the entity:

<models>
    <hwcustomer>
        <class>YourCompany_Hwcustomer_Model</class>
        <resourceModel>hwcustomer_resource</resourceModel>
    </hwcustomer>
    <hwcustomer_resource>
        <class>YourCompany_Hwcustomer_Model_Entity</class>
        <entities>
            <messages>
                <table>hw_messages</table>
            </messages>

In this case hwcustomer/messages maps to the table hw_messages. In writing the SQL directly you likely helped avoid this issue because you're not using a resource locator to find the table name.

Related Topic