4

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.

7

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')
3

I haven't dont this in a while, and I haven't checked my work, BUT

When you say

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

You're telling Magento

Look at the resource model for awesome_ness, and within this resource model tell me the table name for the ness entity.

So, you're basically running the same create table statement, which is why MySQL complains about the table already existing (because it does).

If you add a new entity to your configuration

    <entities>
        <ness>
            <table>awesome_ness</table>
        </ness>
        <sauce>
            <table>awesome_sauces</table>
        </sauce>
    </entities>

You should be able to say

$installer->getTable('awesome_ness/sauce')
  • Answered by the legend. Thank you Alan, I got it now. – Jongosi Jun 6 '13 at 10:59

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.