Magento – Define Multiple Table Names In XML Config File

configurationmodule

I am creating two database tables as part of a new extension. I can create one table just fine, but I get the following error message when I try to create two tables:

PDOException: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'awesome_ness' already exists

Here's my config.xml entry:

<models>
    <awesome_ness>
        <class>Awesome_Ness_Model</class>
        <resourceModel>ness_resource</resourceModel>
    </awesome_ness>
    <ness_resource>
        <class>Awesome_Ness_Model_Resource</class>
        <entities>
            <ness>
                <table>awesome_ness</table>
            </ness>
        </entities>
    </ness_resource>
</models>

In my install-1.0.0.php script, I have two createTable definitions, like so:

<?php
$installer = $this;

$installer->startSetup();

$table_one = $installer->getConnection()
    ->newTable($installer->getTable('awesome_ness/ness'))
    ->addColumn('...');
$installer->getConnection()->createTable($table_one);

$table_two = $installer->getConnection()
    ->newTable($installer->getTable('awesome_ness/ness'))
    ->addColumn('...');
$installer->getConnection()->createTable($table_two);

$installer->endSetup();

I guess I need to know how to create two <table></table> definitions. Thanks in advance.

Best Answer

The thing is, you are trying to create two tables for the same entity. And since an entity can have one table name (which is taken from the xml), you will be creating two tables with the same name. You would need to supply a second entity entry in your XML to create that second table.

<entities>
    <ness>
        <table>awesome_ness</table>
    </ness>
    <foo>
        <table>awesome_foo<table>
    </foo>
</entities>

Then in your install script, for the second table, you call

$installer->getTable('awesome_ness/foo')

instead of

$installer->getTable('awesome_ness/ness')