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.
Best Answer
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