· 7 years ago · Feb 04, 2019, 07:06 PM
1<?php
2/**
3 * @author Convert Team
4 * @copyright Copyright (c) Convert (http://www.convert.no/)
5 */
6
7namespace ConvertPhysicalStoreStockSetup;
8
9use MagentoFrameworkSetupInstallSchemaInterface;
10use MagentoFrameworkSetupModuleContextInterface;
11use MagentoFrameworkSetupSchemaSetupInterface;
12
13/**
14 * Class InstallSchema
15 * @package ConvertPhysicalStoreStockSetup
16 */
17class InstallSchema implements InstallSchemaInterface
18{
19 /**
20 * @param SchemaSetupInterface $setup
21 * @param ModuleContextInterface $context
22 */
23 public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
24 {
25 /**
26 * Create table 'product_store'
27 */
28
29
30 $table = $setup->getConnection()
31 ->newTable($setup->getTable('product_store'))
32 ->addColumn(
33 'entity_id',
34 MagentoFrameworkDBDdlTable::TYPE_INTEGER,
35 null,
36 ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
37 'Entity ID'
38 )
39 ->addColumn(
40 'product_id',
41 MagentoFrameworkDBDdlTable::TYPE_INTEGER,
42 10,
43 ['nullable' => false],
44 'Product ID'
45 )
46 ->addColumn(
47 'stockist_id',
48 MagentoFrameworkDBDdlTable::TYPE_INTEGER,
49 null,
50 ['nullable' => false],
51 'Stockist Id'
52 )
53 ->addColumn(
54 'quantity',
55 MagentoFrameworkDBDdlTable::TYPE_INTEGER,
56 null,
57 ['nullable' => false],
58 'Quantity Value'
59 )
60 ->addForeignKey(
61 $setup->getFkName(
62 'product_store',
63 'product_id',
64 'catalog_product_entity',
65 'entity_id'
66 ),
67 'entity_id',
68 $setup->getTable('catalog_product_entity'),
69 'entity_id'
70 )
71 ->setComment("Relations between stockists and products");
72 $setup->getConnection()->createTable($table);
73 }
74}
75
76SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint, query was: CREATE TABLE IF NOT EXISTS `product_store` (
77 `entity_id` int UNSIGNED NOT NULL auto_increment COMMENT 'Entity ID' ,
78 `product_id` int NOT NULL COMMENT 'Product ID' ,
79 `stockist_id` int NOT NULL COMMENT 'Stockist Id' ,
80 `quantity` int NOT NULL COMMENT 'Quantity Value' ,
81 PRIMARY KEY (`entity_id`),
82 CONSTRAINT `PRODUCT_STORE_PRODUCT_ID_CATALOG_PRODUCT_ENTITY_ENTITY_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE NO ACTION
83) COMMENT='Relations between stockists and products' ENGINE=innodb charset=utf8 COLLATE=utf8_general_ci