0

I just converted the Magento 1(created by Ultimate Module Creator) module Install script for the new magento 2 Install-Schema script, here I got some trouble.

The referencing fields also has the auto increment. Please look at the below code that should make some sense.

$table = $installer->getConnection()
        ->newTable($installer->getTable('vendor_blog_blog_comment'))
        ->addColumn('comment_id', \Magento\Framework\Db\Ddl\Table::TYPE_INTEGER, null, [
                'identity'  => true,
                'nullable'  => false,
                'primary'   => true,
            ], 'Blog Comment ID')
        ->addColumn('blog_id', \Magento\Framework\Db\Ddl\Table::TYPE_INTEGER, null, [
                'identity'  => true,
                'nullable'  => false,
                'primary'   => true,
            ], 'Blog ID')
        ->addColumn('title', \Magento\Framework\Db\Ddl\Table::TYPE_TEXT, 255, [
                'nullable'  => false,
            ], 'Comment Title')
        ->addColumn('comment', \Magento\Framework\Db\Ddl\Table::TYPE_TEXT, '64k', [
                'nullable'  => false,
            ], 'Comment')
        ->addColumn('status', \Magento\Framework\Db\Ddl\Table::TYPE_SMALLINT, null, [
                'nullable'  => false,
            ], 'Comment status')
        ->addColumn('customer_id', \Magento\Framework\Db\Ddl\Table::TYPE_INTEGER, null, [
                'identity' => true, 
                'unsigned' => true, 
                'nullable' => false, 
                'primary' => true
            ], 'Customer id')
        ->addColumn('name', \Magento\Framework\Db\Ddl\Table::TYPE_TEXT, 255, [
                'nullable'  => false,
            ], 'Customer name')
        ->addColumn('email', \Magento\Framework\Db\Ddl\Table::TYPE_TEXT, 255, [
                'nullable'  => false,
            ], 'Customer email')
        ->addColumn('updated_at', \Magento\Framework\Db\Ddl\Table::TYPE_TIMESTAMP, null, [], 'Blog Comment Modification Time')
        ->addColumn('created_at', \Magento\Framework\Db\Ddl\Table::TYPE_TIMESTAMP, null, [], 'Blog Comment Creation Time')
        ->addForeignKey(
            $installer->getFkName(
                'vendor_blog_blog_comment',
                'blog_id',
                'vendor_blog_blog',
                'entity_id'
            ),
            'blog_id', $installer->getTable('vendor_blog_blog'), 'entity_id',
            \Magento\Framework\Db\Ddl\Table::ACTION_CASCADE, \Magento\Framework\Db\Ddl\Table::ACTION_CASCADE)
        ->addForeignKey(
            $installer->getFkName(
                'vendor_blog_blog_comment',
                'customer_id',
                'customer_entity',
                'entity_id'
            ),
            'customer_id', $installer->getTable('customer_entity'), 'entity_id',
            \Magento\Framework\Db\Ddl\Table::ACTION_SET_NULL, \Magento\Framework\Db\Ddl\Table::ACTION_CASCADE);

I just need a way to sort out. If I am wrong somewhere please figure it out.

1

Your action for the 'customer_id' field is ACTION_SET_NULL but that field was defined as not nullable in your script: 'nullable' => false

Try changing 'nullable' => false to 'nullable' => true

0

addForeignKey, accepts 5 params,

* @param string $fkName        the foreign key name
* @param string $column        the foreign key column name
* @param string $refTable      the reference table name
* @param string $refColumn     the reference table column name
* @param string $onDelete      the action on delete row

You have 6. Please modify to

->addForeignKey(
        $installer->getFkName(
            'vendor_blog_blog_comment',
            'blog_id',
            'vendor_blog_blog',
            'entity_id'
        ),
        'blog_id', $installer->getTable('vendor_blog_blog'), 'entity_id',
        \Magento\Framework\Db\Ddl\Table::ACTION_CASCADE)
    ->addForeignKey(
        $installer->getFkName(
            'vendor_blog_blog_comment',
            'customer_id',
            'customer_entity',
            'entity_id'
        ),
        'customer_id', $installer->getTable('customer_entity'), 'entity_id',
        \Magento\Framework\Db\Ddl\Table::ACTION_SET_NULL);

You had \Magento\Framework\Db\Ddl\Table::ACTION_CASCADE twice in the first Foreign key and \Magento\Framework\Db\Ddl\Table::ACTION_CASCADE, \Magento\Framework\Db\Ddl\Table::ACTION_SET_NULL in the second.

  • Param was not an issue in my case because for previous table I had the same parameter and it does works. it seems the Reference column Auto increment was the problem,m please look over "blog_id" and "customer_id" column both were the foreign key Column from Blog table and customer table which should seems to have exactly the same type and configuration as main table column. – Dev Jan 17 '16 at 15:15

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.