Skip to content

Transaction solution#168

Open
dimonalek wants to merge 1 commit into
mate-academy:mainfrom
dimonalek:main
Open

Transaction solution#168
dimonalek wants to merge 1 commit into
mate-academy:mainfrom
dimonalek:main

Conversation

@dimonalek
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Your solution is approved because it fulfills the main task: you create an order for customer ID 1 on 2023-01-01, then within a transaction insert a single OrderItems row for product 1 with count 1 and decrease WarehouseAmount accordingly. One thing to double-check is that your database schema for OrderItems actually defines a Count column; if the table only has ID, OrderID, and ProductID, you should either add Count to the schema or adjust your INSERT column list so it matches. Also be aware that inserting into Orders and calling LAST_INSERT_ID() outside the transaction means that if the later transaction fails, an empty order row will remain; for more robust behavior you could move the order creation into the same transaction or clearly document that this behavior is intended. As a small robustness improvement, consider updating Products.WarehouseAmount by the actual ordered quantity (for example, the Count value or a variable) instead of a hardcoded 1, and optionally add a guard so stock is not decremented below zero. Overall, the core logic and transactional usage match the requirements, and with these refinements your script will be more resilient and aligned with typical production practices.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment thread task.sql
Comment on lines +14 to +15
INSERT INTO OrderItems (OrderID, ProductID, Count)
VALUES (@OrderID, 1, 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The INSERT references a Count column in OrderItems. Verify that your database schema (create-database.sql) defines this Count column; the schema shown in the description lists OrderItems as ID, OrderID, ProductID and an absent Count column will cause this INSERT to fail. If the column is missing, either add it to the schema or adjust the INSERT accordingly.

Comment thread task.sql
INSERT INTO Orders (CustomerID, Date)
VALUES (1, '2023-01-01');

SET @OrderID = LAST_INSERT_ID();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You call SET @OrderID = LAST_INSERT_ID(); after inserting the order outside the transaction. That means if the transaction that inserts the order item and updates stock rolls back, the earlier empty order will remain. Consider creating the order inside the same transaction (or implementing cleanup) unless leaving an empty order on failure is intentional and acceptable.

Comment thread task.sql
VALUES (@OrderID, 1, 1);

UPDATE Products
SET WarehouseAmount = WarehouseAmount - 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UPDATE Products statement decrements WarehouseAmount by a hardcoded 1. This is correct for the required count = 1, but for robustness prefer to use the actual ordered quantity (e.g., subtract the Count value or a variable) so the stock change always matches the order item quantity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants