magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Integrity constraint violation when filtering sales order grid by date

Open cjohnsen1 opened this issue 1 year ago • 20 comments

Preconditions and environment

Magento 2.4.7. I have reproduced this issue on an upgrade from 2.4.6-p3 as well as a clean install of 2.4.7.

Steps to reproduce

  1. Navigate to storefront admin.
  2. Navigate to Sales/Orders.
  3. Click "Filters".
  4. Select a "From" and "To" purchase date.
  5. Click "Apply Filters"

Expected result

Order grid is updated with a list of orders placed during the time period selected.

Actual result

Dialog "Attention: Something went wrong." along with a message stating "Something went wrong with processing the default view and we have restored the filter to its original state."

Additional information

Error Message (Store URL redacted):

Next Zend_Db_Statement_Exception: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous, query was: SELECT main_table.*, braintree_transaction_details.transaction_source, sales_order.dispute_status FROM sales_order_grid AS main_table LEFT JOIN braintree_transaction_details ON braintree_transaction_details.order_id = main_table.entity_id LEFT JOIN sales_order ON sales_order.entity_id = main_table.entity_id WHERE (((created_at >= '2024-06-03 05:00:00'))) AND (((created_at <= '2024-06-06 04:59:59'))) ORDER BY main_table.increment_id DESC LIMIT 20 in /var/www/vhosts/[storeURL]/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:109

Release note

No response

Triage and priority

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [X] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

cjohnsen1 avatar Jun 10 '24 19:06 cjohnsen1

Hi @cjohnsen1. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Jun 10 '24 19:06 m2-assistant[bot]

@magento give me 2.4-develop instance

cjohnsen1 avatar Jun 10 '24 19:06 cjohnsen1

Hi @cjohnsen1. Thank you for your request. I'm working on Magento instance for you.

Hi @cjohnsen1, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

@magento give me 2.4-develop instance

cjohnsen1 avatar Jun 10 '24 19:06 cjohnsen1

Hi @cjohnsen1. Thank you for your request. I'm working on Magento instance for you.

Hi @cjohnsen1, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

Hi @engcom-Delta. 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: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas 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.

m2-assistant[bot] avatar Jun 10 '24 20:06 m2-assistant[bot]

@magento give me 2.4-develop instance

cjohnsen1 avatar Jun 10 '24 20:06 cjohnsen1

Hi @cjohnsen1. Thank you for your request. I'm working on Magento instance for you.

Hi @cjohnsen1, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

Hi @engcom-Dash. 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: :point_down:

    1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    1. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
    1. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
    1. 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!

m2-assistant[bot] avatar Jun 11 '24 08:06 m2-assistant[bot]

Hi @cjohnsen1

Thanks for reporting and collaboration.

Verified the issue on Magento 2.4 dev and 2.4.7 instances but the issue is not reproducible.

We can see the filtered orders.

Steps to reproduce:

  1. Navigate to storefront admin.
  2. Navigate to Sales/Orders.
  3. Click "Filters".
  4. Select a "From" and "To" purchase date.
  5. Click "Apply Filters"

Please refer to the screen-recording and let us know if we missed anything.

https://github.com/magento/magento2/assets/60198592/7d18c869-be0e-4bb1-ae94-87f96be6e548

38818_2

engcom-Dash avatar Jun 11 '24 09:06 engcom-Dash

We experience this issue in one of our instances too after upgrading to v2.4.7. The issue can be solved by disabling all Braintree Extensions.

netzkollektiv avatar Jun 20 '24 09:06 netzkollektiv

It looks like it is any module that adds columns to the sales order and does something with date. In our case it is a GLS module. And in 2.4.4 no problem, as of 2.4.6 this error pops up.

In the case of GLS the code is as follows:

public function afterGetReport($subject, $collection, $requestName)
    {
        if ($requestName !== 'sales_order_grid_data_source') {
            return $collection;
        }
        if ($collection->getMainTable() === $collection->getResource()->getTable('sales_order_grid')) {
            try {
                $GLSTable = $collection->getResource()->getTable('GLS');

                $collection->getSelect()->joinLeft(
                    ['GLS' => $GLSTable],
                    'main_table.entity_id = GLS.GLS_order_id',
                    ['GLS_order_id','GLS_pickup_label','GLS_pickup_id','GLS_message','GLS_status']
                );

                $this->logger->error( $collection->getSelect() );
            } catch (\Zend_Db_Select_Exception $selectException) {
                // Do nothing in that case
               $this->logger->error($selectException);
            }
        }

        return $collection;
    }

So we already have the filtered collection (filtered on created_at > a-certain-date) and we do a left join and then find out the created_at is not defined as belonging to main_table (being sales_order_grid)

So in the end it was not GLS but a custom module that added an attribute to the sales_order_grid the wrong way... we disabled that custom module and the problem was solved.

I used bin/magento dev:query-log:enable to help me find the problem. I hope it may help others.

SolsWebdesign avatar Jun 25 '24 13:06 SolsWebdesign

vendor/paypal/module-braintree-core/Plugin/SalesOrderGridPlugin.php causing error

Braintree using left join on sales_order table. Conflict with sales_order.created_at and sales_order_grid.created_at

public function beforeLoad(Collection $subject, bool $printQuery = false, bool $logQuery = false): array
    {
        if (!$subject->isLoaded()) {
            $primaryKey = $subject->getResource()->getIdFieldName();
            $tableName = $subject->getResource()->getTable('braintree_transaction_details');
            $salesOrderTable = $subject->getResource()->getTable('sales_order');

            $subject->getSelect()->joinLeft(
                $tableName,
                $tableName . '.order_id = main_table.' . $primaryKey,
                $tableName . '.transaction_source'
            );
            $subject->getSelect()->joinLeft(
                $salesOrderTable,
                $salesOrderTable . '.entity_id = main_table.' . $primaryKey,
                $salesOrderTable . '.dispute_status'
            );
        }

        return [$printQuery, $logQuery];
    }

EA-SK avatar Jun 28 '24 06:06 EA-SK

@magento give me 2.4-develop instance

engcom-Dash avatar Jun 28 '24 09:06 engcom-Dash

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

Hi @engcom-Dash, here is your Magento Instance: https://99e1d2483016f2343c57025f7b548cc7.instances-prod.magento-community.engineering Admin access: https://99e1d2483016f2343c57025f7b548cc7.instances-prod.magento-community.engineering/admin_ff1c Login: d945b30d Password: b7fc9f7ad4da

Hi @cjohnsen1

Verified the issue on Magento 2.4.7-p1 and we can reproduce the issue. However, we are not able to reproduce the issue on the 2.4 dev instance.

We can see the filtered orders.

Steps to reproduce:

Navigate to storefront admin. Navigate to Sales/Orders. Click "Filters". Select a "From" and "To" purchase date. Click "Apply Filters"

Please refer to the screenshots attached. Let us know if we missed anything.

In 2.4.7-p1

38818_1 38818-2

In 2.4 dev

Screenshot 2024-06-28 at 3 34 16 PM

engcom-Dash avatar Jun 28 '24 10:06 engcom-Dash

Hi @cjohnsen1

As there is no activity on this issue for a long time, we believe the issue has been resolved, hence closing this issue. Feel free to raise a new issue or reopen this if you need more assistance.

Thank y

engcom-Dash avatar Jul 09 '24 06:07 engcom-Dash

@engcom-Dash the issue hasn't been resolved. It's still a problem in 2.4.7-p1. As I understand it's been already fixed in 2.4-dev so no further steps are to be done. My sad experience with things "fixed in 2.x-dev" is that it sometimes can takes years (yes, years) until they actually get released. So a quality patch would be a lot of help. May we hope for a quality patch being published here: https://github.com/magento/quality-patches ?

xpoback avatar Jul 16 '24 07:07 xpoback

@engcom-Dash where this was fixed?

Bashev avatar Jul 24 '24 09:07 Bashev

Fixed in https://github.com/magento/magento2/pull/36818

Bashev avatar Jul 24 '24 20:07 Bashev

Solved it by disabling the braintree plugin. Create etc/adminhtml/di.xml in your module with this content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Disable braintree Sales order grid plugin -->
    <type name="Magento\Sales\Model\ResourceModel\Order\Grid\Collection">
        <plugin name="braintreeSalesOrderGrid" disabled="true"/>
    </type>
</config>

mariusboia avatar Aug 30 '24 11:08 mariusboia

@Bashev any idea if this fix was released? if yes then in which magento version? I'm still experiencing the same issue with version 2.4.7-p2 (even with a fresh magento install)

Yes as @mariusboia said if the braintreeSalesOrderGrid plugin is disabled the issue is solved, but I don't wanna disable it

amjedomar avatar Oct 16 '24 17:10 amjedomar

Will be in 2.4.8 (actually, it is merged already in 2.4.8-beta1)

Bashev avatar Oct 16 '24 17:10 Bashev

Did anyone apply the solution present here: https://github.com/magento/magento2/pull/36818/files and confirm if the fix is actually working!

rishibankaradicle avatar May 23 '25 06:05 rishibankaradicle

It works.

Bashev avatar May 23 '25 06:05 Bashev

@Bashev I am unable to reproduce the issue while applying the date filter under sales > invoices. Is the issue only occur when we filter the sales > order?

@Bashev Is there a patch available for the above fix as I am currently on 2.4.7-p5 but the fix is available on 2.4.8?

rishibankaradicle avatar May 23 '25 06:05 rishibankaradicle