Menu

1 / 2

Why Adding a Unique SKU Field Broke My Django Migration

DEV Community·Michael Nyawade·6 months ago
#1gpYI7P3
#backend#database#models#enter#exit
Reading 0:00
15s threshold

In building projects using Django, one thing that I have realized about database migrations is that things may appear simple until the moment one alters an existing model.

For example, in developing an inventory management app for my project recently, I experienced a migration problem, which, although small, helped me understand how migrations work within Django and in particular, when adding unique constraints to existing databases.

I would like to document this problem since someone else may have experienced the same thing.

The Initial Inventory Model

At the beginning, my Product model was simple.

from django.db import models

class Product ( models . Model ): name = models . CharField ( max_length = 100 ) buying_price = models . DecimalField ( max_digits = 10 , decimal_places = 2 ) selling_price = models . DecimalField ( max_digits = 10 , decimal_places = 2 ) stock_quantity = models . PositiveIntegerField ( default = 0 ) low_stock_threshold = models .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.