I have created multiple table schema using setup script like below.
Setup/InstallSchema.php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup,
ModuleContextInterface $context)
{
/**
* Create table 'tbl_test1'
*/
$table = $setup->getConnection()
->newTable($setup->getTable('tbl_test1'))
->addColumn(
'id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Pod ID'
)
->addColumn(
'name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'name'
);
$setup->getConnection()->createTable($table);
/**
* Create table 'tbl_test_2'
*/
$table = $setup->getConnection()
->newTable($setup->getTable('tbl_test_2'))
->addColumn(
'id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
''
)
->addColumn(
'mage_order_id',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'magento orderid'
);
$setup->getConnection()->createTable($table);
I need to insert multiple records for each table.
How this can be achieved using installer. Can anyone look into this and update me your answer please.
Best Answer
You can use an
InstallData script
Try this:
Note: Although
id
is defined asnullable=false
you can still put value forid
as NULL if it defined asprimary key
andautoincrement
value.Please Note also, that InstallData script only works on the first install of your module, so better uninstall and remove first the module on your setup_module table to make it work.