2

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?

  • You are setting identity and primary options for 2 columns. I am not sure if Magento can handle multi column primary keys. – A. Capella Jun 10 '16 at 22:46
1

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.

| improve this answer | |
1

I found two ways to fix this issue. In the following section:

->addColumn('original_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null,array(
    'identity'  => true,
    'unsigned'  => true,
    'nullable'  => false,
    'primary'   => true,

I took out the following lines.

    "identity'  => true,

and

    'primary'   => true,

Another way to fix it is to replace the entire function with raw SQL queries so it looks like this. Please note that I changed the message_id field to be called entity_id. I also added some new fields that I didn't realize I would need.

<?php

$installer = $this;

$installer = $startSetup();
$installer->run("

DROP TABLE IF EXISTS customer_messages;  
CREATE TABLE customer_messages (
  `entity_id` int(11) unsigned NOT NULL auto_increment,
  `customer_id` int(11) unsigned NOT NULL,
  `subject` varchar(255) NOT NULL default 'Subject',
  `message` text NOT NULL,
  `reply` varchar(255) NOT NULL default 'reply',
  `status` varchar(255) NOT NULL default 'Status',
  `original_id` int(11) unsigned NOT NULL,
  `original_customer_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`entity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ");


$installer->endSetup();

?>
| improve this answer | |
  • @RaphaelatDigitalPianism I have updated the answer. – djames Jun 14 '16 at 23:23

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.