Magento – Magento 2.1: Not add table into database

magento2setup-script

I follow this tutorial:
https://www.pierrefay.com/en/magento2-training/install-upgrade-setup.html
but i didn't add table into database, i had do anything in tutorial.
From here:
Go to /app/code/Pfay/Contacts/etc/module.xml and verify that the version of our module magento2 is 0.1.0.

<module name="Pfay_Contacts" setup_version="0.1.0">

Creating our installation process for magento2
Now we will create the file that will be launched during the installation of our module, under magento it is always called in the same way: InstallSchema and when called it executes the install function.
Create the file /app/code/Pfay/Contacts/Setup/InstallSchema.php like this:

<?php

namespace Pfay\Contacts\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @throws \Zend_Db_Exception
     */

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /**
         * Create table 'pfay_contacts'
         */

        if (!$setup->getConnection()->isTableExists($setup->getTable('pfay_contacts'))) {
            $table = $setup->getConnection()
                ->newTable($setup->getTable('pfay_contacts'))
                ->addColumn(
                    'pfay_contacts_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                    'Contacts ID'
                )
                ->addColumn(
                    'name',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    100,
                    ['nullable' => false, 'default' => 'simple'],
                    'Name'
                )
                ->addColumn(
                    'email',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    100,
                    ['nullable' => false, 'default' => 'simple'],
                    'Email'
                )
                ->setComment('Pfay Contacts Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');

            $setup->getConnection()->createTable($table);
        }
        $setup->endSetup();
    }
}

In this setup, we create the table in database that will be used for the continuation of the tutorials.

You notice the getTable function that just makes sure to format the table name, no need to declare pfay_contacts elsewhere (as on magento1).
This creates the table with the newTable function and adds columns with addColumn , a comment with setComment
And we specify options linked to the database with setOption
The table is then stored in the database from the connection and the createTable function.

As for magento1, an installer starts with startSetup and ends with endSetup

Enable the module, update the tables and assign the rights to the cache
Via the following commands:

php bin/magento module:enable Pfay_Contacts
php bin/magento setup:upgrade 
chmod 777 -R var/

Can anyone help me with this?

Best Answer

That is better if we take a look at the database after running setup upgrade command. If our custom table cannot be added to database. We need to check again.

  • Remember to remove the setup module from table setup_module.
  • Run setup upgrade command again.

You should read more: https://magento.stackexchange.com/a/150187/33057. The install data script of module only affect your database when running setup command at the first time to set up the module. Changing the setup version number in module xml doesn't apply for install script also. Need to remove setup_module and try again.

There is a note: you also check the existing table $setup->getConnection()->isTableExists($setup->getTable('pfay_contacts')). The new table will not be created if there is the existing one.