how to create table in magento the below file is not able to execute

//This sql file which is created is executed when any magento page is opened in the browser
$installer = $this;  //Getting Installer Class Object In A Variable
$installer->startSetup();//startSetup() runs the sql script
//here is the function used for table name $this->getTable(‘test’)
-- DROP TABLE IF EXISTS {$this->getTable('test')};
CREATE TABLE {$this->getTable('test')} (
  `test_id` int(11) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL default '',
  `filename` varchar(255) NOT NULL default '',
  `content` text NOT NULL default '',
  `status` smallint(6) NOT NULL default '0',
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  PRIMARY KEY (`test_id`)


config file

<?xml version="1.0"?>
    <!--declaring configration of our module-->
            <!-- Version of module -->
        <!--front end refers to magento area-->
            <!--routers will turn url into actioncontroller and method-->
            <test><!--this should be lower case name of your module and this defines our route name-->
                    <!--module will be used by the system to locate your Controller files.-->
                    <!--this is the front name which will appear in url-->
        <layout><!--defining the layout of our web page-->
                    <file>test123.xml</file><!-- This is name of the layout file for this module -->
        <!--Enabling Model Resources in our Module-->
        <models><!--Model are used to logical + data operations in MVC pattern
        There are two parts to models in magento, 1st is the Model file and 2nd Model resource files. Models are used to for logical operation and resource files are used to perform actual database queries-->
            <test><!--this is model group name , which should match your module name-->
                <class>Excellence_Test_Model</class> <!-- Location of all model class files -->
                <!--need to create our own resource, can't just use core_mysql4  -->
                <resourceModel>test_mysql4</resourceModel> <!-- Location of resource model -->
                <!--The <resourceModel /> tag indicates which Resource Model that test group Models should use-->
                    <!--Here basically, we add a list of all our tables we would like to use in our module.
                    if more than one table is added then code should be like this
                    <test><!--here we have added only one table  iw pfay_test-->
                        <table>pfay_test</table>  <!-- Actual table name in sql  -->
        <resources>  <!-- These are resource setting giving access to module, read/write permission on database -->
                <class>Excellence_Test_Block</class><!-- Path of the Block Folder, where all php files are located related to view -->
                <class>Excellence_Test_Helper</class><!-- Path of Helper Files -->
  • please provide us with your config.xml as well Commented Nov 15, 2014 at 9:12
  • 1
    What is the file name and path of your install script? Commented Nov 15, 2014 at 14:02

2 Answers 2


Since Magento 1.6 the Resource alias has been renamed from mysql4 to resource, also the way to create a table has changed from direct SQL queries to Varien_Db_Ddl_Table. This gives you a uniform way to create tables, indexes and foreign-keys. Below a sample on how to create:


<?xml version="1.0" encoding="UTF-8"?>
            <!-- define version for sql upgrade -->
            <!-- initialize sql upgrade setup -->

Setup script:


$installer = $this;


$table = $installer->getConnection()
            'identity' => true,
            'unsigned' => true,
            'nullable' => false,
            'primary' => true,
            'auto_increment' => true,
        'Entity Id'
    ->addColumn('title', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(), 'Title')
    ->addColumn('filename', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(), 'Filename')
    ->addColumn('content', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(), 'Content')
    ->addColumn('status', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(), 'Status')
    ->addColumn('created_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(), 'Create date/time')
    ->addColumn('update_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(), 'Update date/time');



Next you will also need the create the Model, Resource Model en Resource Collection files so that you can use the model demotable.


class Example_SalesGrid_Model_Demotable extends Mage_Core_Model_Abstract
    protected function _construct()


class Example_SalesGrid_Model_Resource_Demotable extends Mage_Core_Model_Resource_Db_Abstract
    protected function _construct()
        $this->_init('example_salesgrid/demotable', 'test_id');


class Example_SalesGrid_Model_Resource_Demotable_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract

    protected function _construct()

There are a set of issues that may occurred and not allowing you to create the table, The XML config file you supported does not seam to have anything wrong, but I would make sure of the following:

  1. Please make sure under your extension directory to have the "sql" directory.
  2. under that make sure that the directory's name is "test_setup"
  3. the sql file inside, if it is a new installation to be mysql4-install-(the version of your extension).php
  4. If everything is there and you still can't create the table, You have to know that Magento has a table called "core_resource" where it stores all extension codes that needs to run sql file, if the code of the extension is saved and the version matches the version of your extension inside the etc/config.xml file , then it will not run the sql again since it considers it ran from before. Solution: You have to check the table core_resource for your setup code; in this example it is "test_setup", delete it, flush cache, and reload Magento, or the admin dashboard. Once reloaded, Magento will check if your extension code is in that database table, if it is not, it will run the sql inside your extension as an initial setup.

Hope this helps.

  • please explain step 4
    – kumar0981
    Commented Nov 16, 2014 at 5:53
  • I updated step 4, please let me know if you need more details.
    – Rabea
    Commented Nov 17, 2014 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.