1
<?php namespace Magneto\BookFreeLayout\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)
 {

 $newsTableName = $setup->getTable('magneto_book_free_layout');

if($setup->getConnection()->isTableExists($newsTableName) != true) {

  $newsTable = $setup->getConnection()
      ->newTable($newsTableName)
      ->addColumn(
          'id',
          \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
          null,
          ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
          'id'
      )
      ->addColumn(
          'name',
          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
          255,
          ['nullable' => false, 'default' => ''],
            'name'
      )
      ->addColumn(
          'email',
          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
          null,
          ['nullable' => false, 'default' => ''],
            'email'
      )
      ->addColumn(
          'phoneno',
          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
          null,
          ['nullable' => false, 'unsigned' => true],
            'phoneno'
      )
      ->addColumn(
          'heigth',
          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
          null,
          ['nullable' => false],
            'heigth'
      )
      ->addColumn(
          'width',
          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
          null,
          ['nullable' => false],
            'width'
      )
      ->addColumn(
          'image',
          \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
          null,
          ['nullable' => false],
            'image'
      )
      ->addColumn(
          'entity_id',
          \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
          null,
          ['nullable' => false],
            'entity_id'
      )
      ->addColumn(
          'status',
          \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
          null,
          ['nullable' => false,'default' => 0],
            'status'
      )
      ->addForeignKey(
            $installer->getFkName(
                'magneto_book_free_layout',
                'entity_id',
                'catalog_product_entity',
                'entity_id'
            ),
            'entity_id',
            $installer->getTable('catalog_product_entity'), 
            'entity_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )
      ->setComment("News Table");



  $setup->getConnection()->createTable($newsTable);
}
}
}
  • Notice: Undefined variable: installer in /var/www/html/magento/app/code/Magneto/BookFreeLayout/Setup/InstallSchema.php on line 84 – Harsh Jayswal Feb 19 at 12:56
  • use $setup variable instead of $installer – Yash Shah Feb 19 at 12:58
  • @YashShah After Using Setup It Shows Error – Harsh Jayswal Feb 19 at 13:02
0

Your entity_id column should look like this.

->addColumn(
      'entity_id',
      \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
      null,
      ['unsigned' => true, 'nullable' => false, 'index' => true],
      'entity_id'
)
  • I Want To Give foreign key to that column if i write primary then it will become primary key – Harsh Jayswal Feb 19 at 13:11
  • Primary and Identity are 2 different concept. Identity is auto increament while primary is not. And the reference table has assigned it as primary so if you want to have foreign key with that table, you must define it as primary as per database concepts or else it wont allow you to do that. – Yash Shah Feb 19 at 13:14
  • Thank Buddy For You Valuable Time It Will Helped Me Alot – Harsh Jayswal Feb 19 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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