Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a custom EAV entity type can cause mysql integrity constraint violations when reinitializing the database #10398

Closed
hostep opened this issue Aug 1, 2017 · 4 comments
Labels
Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed

Comments

@hostep
Copy link
Contributor

hostep commented Aug 1, 2017

Preconditions

  1. Tested with Magento CE 2.1.7 and Magento 2.2.0-preview branch (commit 22b7543)
  2. PHP 7.0.20
  3. MariaDB 10.2.7

Steps to reproduce

  1. Setup a new Magento installation
  2. Create a module which introduces a new Entity Type, make sure the name of the module comes alphabetically before 'Magento_', I've used 'A_B' (see the bottom for a small example)
  3. Run: bin/magento setup:install with the --cleanup-database flag

Expected result

  1. Installation runs without errors

Actual result

  1. During installation of the Magento_Sales module, an error is thrown:
...
[Progress: 282 / 470]
Module 'Magento_Sales':
Installing data...

  [Magento\Framework\DB\Adapter\DuplicateException]
  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5' for key 'PRIMARY', query was: INSERT INTO `eav_entity_type` (`entity_type_code`, `entity_model`, `attribute_model`, `entity_table`, `value_table_prefix`, `enti
  ty_id_field`, `increment_model`, `increment_per_store`, `increment_pad_length`, `increment_pad_char`, `additional_attribute_table`, `entity_attribute_collection`, `entity_type_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Discussion

This problem happens because certain Magento modules (Magento_Customer, Magento_Catalog & Magento_Sales) use a hardcoded entity_type_id in their database installation scripts.
In this case, the custom module gets installed before the Magento_Sales module and adds an eav entity type with id '5'. The Magento_Sales module then tries to install itself, but fails to do, because it wants to use id '5' as well, but this one is already taken by the custom module.

We can fix it by adding 'Magento_Sales' in the sequence of our custom module, but this doesn't make much sense to me, since the custom module is not depending on the 'Magento_Sales' module for any functionality.

Is this considered a bug, or should something like this be better documented?

I'm wondering why the Magento_Customer and Magento_Catalog module's setups are being executed before our custom module though, if somehow Magento_Sales could also be "automagically" be executed before a custom module who wants to create an Entity Type that would fix the bug, but this is probably not so easy?

Example module files

  • app/code/A/B/registration.php:
<?php

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'A_B', __DIR__);
  • app/code/A/B/etc/module.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="A_B" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Eav"/>
            <!-- Uncomment this to fix the issue -->
            <!-- <module name="Magento_Sales"/> -->
        </sequence>
    </module>
</config>
  • app/code/A/B/Model/ResourceModel/Example.php:
<?php

namespace A\B\Model\ResourceModel;

use \Magento\Eav\Model\Entity\AbstractEntity;

class Example extends AbstractEntity
{

}
  • app/code/A/B/Setup/ExampleSetup.php:
<?php

namespace A\B\Setup;

use \Magento\Eav\Setup\EavSetup;

class ExampleSetup extends EavSetup
{
    public function getDefaultEntities()
    {
        return [
            'example_entity' => [
                'entity_model' => 'A\B\Model\ResourceModel\Example',
                'table'        => 'example_entity',
                'attributes'   => [
                    'some_attribute' => [
                        'type' => 'varchar'
                    ]
                ]
            ]
        ];
    }
}
  • app/code/A/B/Setup/InstallData.php:
<?php

namespace A\B\Setup;

use \Magento\Framework\Setup\InstallDataInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;
use \Magento\Framework\Setup\ModuleContextInterface ;

class InstallData implements InstallDataInterface
{
    private $exampleSetupFactory;

    public function __construct(ExampleSetupFactory $exampleSetupFactory)
    {
        $this->exampleSetupFactory = $exampleSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->exampleSetupFactory->create(['setup' => $setup])->installEntities();
    }
}
@magento-engcom-team magento-engcom-team added G1 Passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed and removed G1 Passed labels Sep 5, 2017
@magento-engcom-team magento-engcom-team self-assigned this Sep 28, 2017
@magento-engcom-team magento-engcom-team added the Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed label Sep 28, 2017
@magento-engcom-team
Copy link
Contributor

@hostep, thank you for your report.
We've created internal ticket(s) MAGETWO-80274 to track progress on the issue.

@magento-engcom-team magento-engcom-team added 2.1.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Sep 28, 2017
@sdzhepa sdzhepa removed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Dec 10, 2019
@engcom-Charlie engcom-Charlie self-assigned this Dec 12, 2019
@m2-assistant
Copy link

m2-assistant bot commented Dec 12, 2019

Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Charlie
Copy link
Contributor

Hello @hostep
Thank you for contribution and collaboration!

We are not able to reproduce this issue on the latest 2.4-develop branch by provided steps.
Result:

  1. No error while Magento_Sales installing
    1

  2. Magento installation complete
    2

We are closing this issue due to branch 2.2-develop was closed and

Magento no longer accepts pull requests and issues to the v2.2 release lines to focus all development efforts on v2.3.

@hostep
Copy link
Contributor Author

hostep commented Jan 4, 2021

This was most likely fixed as a side-effect of #21020 or #21423 where the sort order of modules has been changed so that Magento modules are now always loaded first and all the rest later. It wouldn't surprise me if that change will get reverted on day or another because it doesn't feel right in my opinion. If that happens one day, then this issue will start happening again.
Anyway, just leaving this comment behind in case this starts happening again in the future some day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed
Projects
None yet
Development

No branches or pull requests

5 participants