Create an OrderItem OR Link a LineItem to an Order in a simple e-commerce database?

Viewed 323

I'm building my own e-commerce system from scratch using the django-rest-framework.

Here is a description of my system architecture:

Db format = PostgresSQL

One Account has many LineItems (aka CartItems).

One LineItem has one Account (FK), one Product (FK)

One Account has many Orders


LineItem Fields:

  • Account FK
  • Product FK
  • Quantity
  • Price (Serializer method field to factor in any discounts (range 0-1) specified the product model).

My Issue lies with the account being able to create an order.

I have identified two ways so far:

1) Modify the LineItem Model by adding the following fields:

  • IsMountedForOrder (for buy now purposes) - BooleanField
  • Order (Foreign Key)

Constraint: The LineItems Model becomes overburdened with Foreign Keys and relationships - it seems like going to be a hindrance from a DB management standpoint. Do correct me if I'm wrong.

2) Create an OrderItem Model (One Order has Many OrderItems)

Fields:

  • Order FK
  • Product FK (Parsed from owned LineItems)
  • Quantity (Parsed from owned LineItems)
  • Price (Parsed from owned LineItems)

and upon creation of a successful order, the lineItems will be deleted.

Constraints: There is a duplication of Data for a short duration and seems like unnecessary computation - therefore adding latency to the service.

I would appreciate any suggestions in how I can achieve an efficient order creation method. Either by modifying the two methods or identifying any architecture flaws I might have overlooked. Thank you!

1 Answers

Depending on the level of complexity in your e-commerce system, I would aim my design to be something similar to this:

1. Account
2. Order { account_id, total, tax, address, etc}
3. OrderLine { order_id, product_id, price, discount, quantity_ordered, quantity_fulfilled, etc}
4. Cart { account_id, subtotal, tax, coupons, is_abandoned, address, etc}
5. CartLine { cart_id, product_id, price, discount, quantity, etc}

Typically you do not want to create OrderLine until an order has been placed. If you're storing Cart information on the server, create a separate table to maintain those.

Once a user adds items to their Cart, update the Cart/CartLine. After they proceed to checkout and are finished with their payment, take the Cart (+CartLine) object and create an Order (+OrderLine) from it.

If you want to decrease this complexity by a bit, I would recommend to not store Cart information in a table, and simply use request.session to store cart info.

When the user completes their order, convert the cart into an Order+OrderLine.

If you want more freedom (with increased complexity) instead of Cart + CartLine, I would use Checkout + CheckoutLine tables which store more information that just the cart (things like which step the customer has reached in their checkout process, payment info, etc.)

I hope that answers some of your queries based on my understanding of your question.

Related