Magento – How to create a table in the database in Magento using install script

magento-1.7magento-1.8magento-1.9

I am new to Magento, and I am trying to create a table in the database using an install script. THE WEB SITE IS HOSTED. I followed few tutorials, and they all look same. I followed every step, but the table is not created. Can some one tell me where I have gone wrong? I created the following file:

app/code/local/Sitepoint/Articles/etc/config.xml

It contains following code:

<global>
    <models>
        <articles>
            <class>Sitepoint_Articles_Model</class> <!-- Model class files -->     
            <resourceModel>articles_mysql4</resourceModel> <!--Resource model -->
        </articles>
        <articles_mysql4>
            <class>Sitepoint_Articles_Model_Mysql4</class>
            <entities>
                <articles>
                    <table>articles</table>  <!-- Db table name  -->
                </articles>
            </entities>
        </articles_mysql4>
    </models>
    <resources>  
        <articles_setup>
            <setup>
                <module>Sitepoint_Articles</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </articles_setup>
        <articles_write>
            <connection>
                <use>core_write</use>
            </connection>
        </articles_write>
        <articles_read>
            <connection>
                <use>core_read</use>
            </connection>
        </articles_read>
    </resources>
</global>

Then I created following file:

app/code/local/Sitepoint/Articles/sql/articles_setup/mysql4-install-0.1.0.php

Which has the following code:

<?php
$installer = $this;
$installer->startSetup();
$installer->run("-- DROP TABLE IF EXISTS {$this->getTable('articles')};
CREATE TABLE {$this->getTable('articles')} (
      `articles_id` int(11) unsigned NOT NULL auto_increment,
      `title` varchar(255) NOT NULL default '',
      `short_desc` text NOT NULL default '',
      `long_desc` text NOT NULL default '',
      `status` tinyint(2) NOT NULL default '0',
      `created_time` datetime NULL,
      `update_time` datetime NULL,
      PRIMARY KEY (`articles_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ");
    $installer->endSetup();
?>

I followed this tutorial => https://www.sitepoint.com/magento-install-upgrade-data-scripts-explained/ but the tables are not created. I tried few other tutorials, and they all provide the same way. Can someone help me?

Best Answer

You have to specify the version of the module in the config.xml in your etc module folder. See below:

<?xml version="1.0"?>
<config>
    <modules>
        <Sitepoint_Articles>
            <version>0.1.0</version> 
        </Sitepoint_Articles>
    </modules>
    <global>
        <models>
            <articles>
                <class>Sitepoint_Articles_Model</class> <!-- Model class files -->
                <resourceModel>articles_mysql4</resourceModel> <!--Resource model -->
            </articles>
            <articles_mysql4>
                <class>Sitepoint_Articles_Model_Mysql4</class>
                <entities>
                    <articles>
                        <table>articles</table>  <!-- Db table name  -->
                    </articles>
                </entities>
            </articles_mysql4>
        </models>
        <resources>
            <articles_setup>
                <setup>
                    <module>Sitepoint_Articles</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </articles_setup>
            <articles_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </articles_write>
            <articles_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </articles_read>
        </resources>
    </global>
</config>

And make sure that you registered the modules from

app/etc/modules/Sitepoint_Articles.xml and with a content:

<?xml version="1.0"?>
<config>
    <modules>
        <Sitepoint_Articles>
        <active>true</active>
        <codePool>local</codePool>
        </Sitepoint_Articles>
    </modules>
</config>

Hope that helps.