Magento 2 REST Search Orders Starting at Order_ID - magento2

I'm writing a Magento2 integration and I'm trying to only get a list of orders by order_id since the last order_id that i have sync'd.
So, my orders search call would only get order_id's greater than the last order id that I've synced (i'll keep this in my app).
In this example, I only want order ID's greater than order_id = 20 and sorting by order_id ASC:
/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=order_id&searchCriteria[filter_groups][0][filters][0][value]=20&searchCriteria[filter_groups][0][filters][0][condition_type]=from[sortOrders][0][field]=order_id&searchCriteria[sortOrders][0][direction]=asc
But i'm getting a 500 error thrown from the REST API when I run this.
Any ideas what is wrong with my call?

I am afraid there is no order_id field in Magento 2 sales_order table. Instead of that you can use entity_id ( the primary key).
So your query url will be below and this will pull the orders greater than supplied order id.
https://yourhost.com/rest/V1/orders?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][value]=179&searchCriteria[filterGroups][0][filters][0][conditionType]=gt
Also post your Magento related queries in here for better visibility

Related

How to Find Magento Missing Order and what is the cause of it

Customer Place order payments received with no corresponding orders. From the customer's perspective, they had submitted an order and paid us but nothing was appearing in our order history on the admin side in the sales order grid, just a skipped increment_id.
I have checked the quote table there is order_reserved_id which is the same as order increment_id but I query the same increment_id in the sales_order table query return null.
Q1: how to find all missing orders.
Q2: How to debug this issue.
Q3: what are the possible causes of this issue?
I Have tried different queries (match the reserved_order_id of quote table with increment_id of sales_order table) to find out the missing entries in the sales_order table. I don't know if this is the right way to find the missing order. we are using the Meetanshi_Paya module as a payment method with Magento 2.4.3.

How to implement GraphQL Relay style cursor based pagination in postgres (with sqlalchemy and fastAPI)

I am trying to implement Relay-Style pagination on a postgres. I have gone through the relay specification, and it recommends having a globally unique cursor for each entity. The specification for pagination is that the client provides first and after for Forward pagination and last and before for Backward pagination.
Assume I have a table called Products, with an auto incrementing primary key integer ID. In the most basic case, pagination is very straight forward. For forward pagination it would be something like SELECT * FROM products WHERE id > cursor LIMIT 10 for forward pagination, and SELECT * FROM products WHERE id < cursor ORDER BY id DESC LIMIT 10
The problem arises when we have additional sorting requirements. Let us say, our product table has a name field on which we want to sort.
Name Id (cursor field)
Trendy 1
Autumn 2
Winter 3
Crazy 4
Antman 5
Marvel 6
And we want to paginate, with page size of 3, sorted on name ASC. The query would be:
SELECT * FROM products ORDER BY name ASC LIMIT 3, And we would get the following output:
(Antman, 5), (Autumn 1), (Crazy 4)
But now, how will I get the next three? This query SELECT * FROM products WHERE id >= 4 ORDER BY name ASC LIMIT 3; would not work, as Trendy has ID < 1. Do I create a different cursor for Name and use that for paginating with Name? Okay, what if we wanted to sort on multiple fields together (let us say we have a price field, we want to sort from decreasing to increasing price, and then sort based on created time, and then on name. The price might not be unique and any other additional constraints), how would I create a cursor then?
What is the standard approach to solve this problem? I have tried to research as much as I can but I couldn't find any information on how to accomplish this in fastapi and sqlalchemy. Django has FilterableConnectionField, but I couldn't understand the implementation. Any help is very appreciated, thank you.
edit: I figured out how to do this. It is called queryset pagination, and sqlakeyset repo on github had a good implementation of this pattern. https://github.com/djrobstep/sqlakeyset

Postgres SELECT id LAG(id) OVER (ORDER BY id) FROM invoice WHERE id = 2

I've looked all over the internet and I fail to get this query running as expected.
I've got a table of invoices and some invoices are related to one another because they belong to the same project.
My ticket says I've got to get the PREVIOUS invoice based on a provided invoice.
Say Project A has 10 invoices, and I'm looking at invoice #4, I've got to write a query which will return the ID of the previous Invoice. Bear in mind, the invoice table is home to all sorts of projects, and each project could have many invoices on their own, so I want to avoid getting many IDs back and then iterating over them.
To illustrate the issue, I've written this fiddle.
It works somewhat acceptably when I don't filter for steps.id, but that means returning hundreds of IDs to sift through.
I've tried and tried but I can't seem to get the column previousStep to be kind of bound to the ID column.
Simply find the invoice with the next largest id for the same project:
SELECT inv2.id
FROM invoice AS inv1
JOIN invoice AS inv2
ON inv1.project = inv2.project AND inv1.id > inv2.id
WHERE inv1.id = 1057638
ORDER BY inv2.id DESC
LIMIT 1;

why is it that WooCommerce REST API returns total orders 0 for a customer but has 1 order for that customer in order table?

I am working on WooCommerce REST API. I am fetching customers and orders. I want to know how WooCommerce calcluates total number of orders as for some customers it is returning less results than the record found in orders table. Is there any issue with API?
here is the customer table result: Customer Table
here is the order table
Orders Table. Sorry stack overflow only allows me to add links of the images.
BTW you can clearly see that customer has one order but in customer table, it shows 0 orders, this data is returned by woocommerce rest api, so there is no chance of error from me. I can't add full image of DB due to some privacy issue and company rules.

PostgreSQL: Returning ordered rows after a specific ID

Scenario:
I am displaying a table of records. It initially displays the first 500 with "show more" at the bottom, which returns the next 500.
Issue:
If between initial display and clicking "show more" 1 record is added, that will cause "order by date, offset 500, limit 500" to overlap by 1 row.
I'd like to "order by date, offset until 'id of last row shown', limit 500"
My row IDs are UUIDs. I am open to alternative approaches that achieve the same result.
If you can order by ID, you can paginate using
where id > $last_seen_id limit 500
but that's not going to be useful where you're sorting by date.
Sort stability!
I really hope that "date" actually means "timestamp" though, otherwise your ordering will be unstable and you can miss rows in pagination; you'll have to order by date, id to get stable ordering if it's really a date, and should probably do so even for timestamp.
State on client
One option is to push the state out to the client. Have the client remember the last-seen (date,id) tuple, and use:
where date > $last_seen_date and id > $last_seen_id limit 500
Cursors
Do you care about scalability? If not, you can use a server-side cursor. Declare the cursor for the full query, without the LIMIT. Then FETCH chunks of rows as requested. To do this your app must have a way to consistently bind a connection to a specific user's requests, though, and not to reset that connection or return it to the pool between requests. This might not be practical with your pool/framework, but is probably the best solution if you can do it.
Temp tables
Another even less scalable option is to CREATE TABLE sessiondata.myuser_myrequest_blah AS SELECT .... then paginate that table. It's guaranteed not to change. This avoids the difficulty of needing to keep a consistent connection across requests, but will have a very slow first-request response time and is completely impractical for large user counts or large amounts of data.
Related questions
Handling paging with changing sort orders
Using "Cursors" for paging in PostgreSQL
How to provide an API client with 1,000,000 database results?
i think you can use a subquery in the where to accomplish this.
e.g. given you're paginating through a users table, and you want the records after a given user:
SELECT *
FROM users
WHERE created_at > (
SELECT created_at
FROM users
WHERE users.id = '00000000-1111-2222-3333-444444444444'
LIMIT 1
)
ORDER BY created_at DESC limit 5;