:::: MENU ::::

Magento 2 Create a install Schema

Magento 2 Create a install Schema

When we create a custom module sometime we need to add the table for that module. So in magento 2 we use install Schema. So today we learn how to in Magento 2 Create a install Schema? First we need to create the folder in our module directory name Setup. The folder location will be.


Now we create our InstallSchema file. The file location will be.


Below is example code to create a table from install Schema.


namespace QaisarSatti\HelloWorld\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface {

    public function install( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
        $installer = $setup;


        $table = $installer->getConnection()->newTable(
            $installer->getTable( 'sample_posts' )
            [ 'identity' => true, 'nullable' => false, 'primary' => true ],
            'Post ID'
            [ 'nullable' => false ],
            'Post Title'
            [ ],
            'Post Content'
            'Sample Post Table'

        $installer->getConnection()->createTable( $table );


Example code is available on Github.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018

So, what do you think ?