Magento – Magento 2 foreign key

databaseinstall-scriptmagento-2.1magento2setup-upgrade

Hey i need to setup the install shema of my module with 2 table and a third table thatmake the link between the two.

First table have id_provider the second id_company i want my third have the wo id.

It is foreign key ?

I do the following :

<?php


namespace Eurecia\Catalog\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\DB\Adapter\AdapterInterface;

class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function install(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $installer = $setup;
        $installer->startSetup();

        // Get eurecia_catalog table
        $tableName = $installer->getTable('eurecia_providers');
        // Check if the table already exists
        if ($installer->getConnection()->isTableExists($tableName) != true) {
            // Create eurecia_catalog table
            $table = $installer->getConnection()
                ->newTable($tableName)
                ->addColumn(
                    'id_provider',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID Provider'
                )
                ->addColumn(
                    'name',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Name'
                )
                ->addColumn(
                    'subdomain',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Subdomain'
                )
                ->addColumn(
                    'summary',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Summary'
                )
                ->addColumn(
                    'address',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Address'
                )
                ->addColumn(
                    'maxdist',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Maxdist'
                )
                ->addColumn(
                    'status',
                    Table::TYPE_SMALLINT,
                    null,
                    ['nullable' => false, 'default' => '0'],
                    'Status'
                )
                ->setComment('Catalog Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');
            $installer->getConnection()->createTable($table);
        }

        // Get eurecia_company table
        $tableName = $installer->getTable('eurecia_company');
        // Check if the table already exists
        if ($installer->getConnection()->isTableExists($tableName) != true) {
            // Create eurecia_company table
            $table = $installer->getConnection()
                ->newTable($tableName)
                ->addColumn(
                    'id_company',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID Company'
                )
                ->addColumn(
                    'name',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Name'
                )
                ->addColumn(
                    'subdomain',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Subdomain'
                )
                ->addColumn(
                    'address',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Address'
                )
                ->addColumn(
                    'status',
                    Table::TYPE_SMALLINT,
                    null,
                    ['nullable' => false, 'default' => '0'],
                    'Status'
                )
                ->setComment('Catalog Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');
            $installer->getConnection()->createTable($table);
        }

        if (!$installer->tableExists('eurecia_catalog_assoc')) {
            $table = $installer->getConnection()
                ->newTable($installer->getTable('eurecia_catalog_assoc'));
            $table->addColumn(
                    'id_company',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'unsigned' => true,
                        'nullable' => false,
                        'primary'   => true,
                    ],
                    'company ID'
                )
                ->addColumn(
                    'id_provider',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'unsigned' => true,
                        'nullable' => false,
                        'primary'   => true,
                    ],
                    'provider ID'
                )
                ->addColumn(
                    'position',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'nullable' => false,
                        'default' => '0'
                    ],
                    'Position'
                )
                ->addIndex(
                    $installer->getIdxName('eurecia_catalog_assoc', ['id_company']),
                    ['id_company']
                )
                ->addIndex(
                    $installer->getIdxName('eurecia_catalog_assoc', ['id_provider']),
                    ['id_provider']
                )
                ->addForeignKey(
                    $installer->getFkName(
                        'eurecia_catalog_assoc',
                        'id_company',
                        'eurecia_company',
                        'id_company'
                    ),
                    'id_company',
                    $installer->getTable('eurecia_company'),
                    'id_company',
                    Table::ACTION_CASCADE,
                    Table::ACTION_CASCADE
                )
                ->addForeignKey(
                    $installer->getFkName(
                        'eurecia_catalog_assoc',
                        'id_provider',
                        'eurecia_providers',
                        'id_provider'
                    ),
                    'id_provider',
                    $installer->getTable('eurecia_providers'),
                    'id_provider',
                    Table::ACTION_CASCADE,
                    Table::ACTION_CASCADE
                )
                ->addIndex(
                    $installer->getIdxName(
                        'eurecia_catalog_assoc',
                        [
                            'id_company',
                            'id_provider'
                        ],
                        AdapterInterface::INDEX_TYPE_UNIQUE
                    ),
                    [
                        'id_company',
                        'id_provider'
                    ],
                    [
                        'type' => AdapterInterface::INDEX_TYPE_UNIQUE
                    ]
                )
                ->setComment('Company to provider Link Table');
            $installer->getConnection()->createTable($table);
        }

        $setup->endSetup();
    }
}

I get an error 1215 in my cmd when i do phpbin/magento setup:upgrade
Error show

I look on forum but i don't know where i am wrong …

Best Answer

I just had a similar problem and found out that when you specify the key column without a length, this error appears.

So to fix it, use a length for the Integer here:

->addColumn(
                'id_company',
                Table::TYPE_INTEGER,
                10,
                [
                    'identity' => true,
                    'unsigned' => true,
                    'nullable' => false,
                    'primary' => true
                ],
                'ID Company'
            )