Can't apply migrations to sql-lite

I am Facing a problem with my project, (cant apply the changes on Django DB)

There was an empty field and it didn’t set it to Null = TRUE, Blank =True, when I migrated it kept giving me this error message :


django.db.utils.IntegrityError: NOT NULL constraint failed: new__orders_order.total

I tried to set the values in the cart model to True and then I changed them to False, It didn’t solve the problem.

I tried importing the Integrity error, still didn’t solve the issue :

   def save(self, *args, **kwargs):
        try:
            super.save(*args, **kwargs)
        except IntegrityError:
            self.code = generate_code()
            super.save(*args, **kwargs)

Models

from django.db import models, IntegrityError
from django.contrib.auth.models import User
from django.utils import timezone
from utils.generate_code import generate_code
from products.models import Product
from accounts.models import Address
import datetime 


ORDER_STATUS = (
    ('Received', 'Received'),
    ('Processed', 'Processed'),
    ('Shipped', 'Shipped'),
    ('Delivered', 'Delivered')
)

class Order(models.Model):
    user = models.ForeignKey(User,related_name='order_owner', on_delete = models.SET_NULL, null = True, blank = True)
    status = models.CharField(choices=ORDER_STATUS, max_length=12)
    code = models.CharField(default=generate_code, max_length=20,unique=True)
    order_time = models.DateTimeField(default=timezone.now)
    delivery_time = models.DateTimeField(blank=True, null=True)
    delivery_address = models.ForeignKey(Address, related_name='delivery_address', on_delete=models.SET_NULL, null=True,blank = True)
    coupon = models.ForeignKey('Coupon', related_name='order_coupon', on_delete=models.SET_NULL, null=True,blank = True)
    total = models.FloatField(null=True,blank = True)
    total_with_coupon = models.FloatField(null=True,blank = True)
    
    def save(self, *args, **kwargs):
        try:
            super.save(*args, **kwargs)
        except IntegrityError:
            self.code = generate_code()
            super.save(*args, **kwargs)

class OrderDetail(models.Model):
    order = models.ForeignKey(Order, related_name='order_detail', on_delete=models.CASCADE)
    product = models.ForeignKey(Product, related_name='orderdetail_product', on_delete=models.SET_NULL, null=True,blank=True)
    qauntity = models.IntegerField()
    price = models.FloatField()
    total = models.FloatField(null = True, blank = True)
    


CART_STATUS = (
    ('Inprogress', 'Inprogress'),
    ('Completed', 'Completed'),
)
class Cart(models.Model):
    user = models.ForeignKey(User,related_name='cart_owner', on_delete = models.SET_NULL, null = True, blank = True)
    status = models.CharField(choices=CART_STATUS, max_length=12)
    coupon = models.ForeignKey('Coupon', related_name='cart_coupon', on_delete=models.SET_NULL, null=True,blank = True)
    total_with_coupon = models.FloatField(null = True,blank = True)
    
    @property
    def cart_total(self):
        total =0
        for item in self.cart_detail.all():
            total += item.total
        return round(total,2)
    

class CartDetail(models.Model):
    order = models.ForeignKey(Cart, related_name='cart_detail', on_delete=models.CASCADE)
    product = models.ForeignKey(Product, related_name='cartdetail_product', on_delete=models.SET_NULL, null=True,blank=True)
    qauntity = models.IntegerField(default=1)
    total = models.FloatField(null = True, blank = True)
    
class Coupon(models.Model):
    code = models.CharField(max_length=20)
    start_date = models.DateField(default=timezone.now)
    end_date = models.DateField(null = True,blank = True)
    quantity = models.IntegerField()
    discount = models.FloatField()
    
def save(self, *args, **kwargs):
    week = datetime.timedelta(days=7)
    self.end_date = self.start_date + week
    super(Coupon, self).save(*args, **kwargs)

Side note - you need to use the backtick - ` character, not the apostrophe - ' when creating the ``` fence around your code. (I’ve taken the liberty of correcting this post for you.)

There are a couple things you can do to help diagnose this:

  • If you’re trying to execute multiple migrations, try running all migrations before the one that is causing the problem.

  • You can use the sqlmigrate command to see exactly what SQL statements are being issued to perform the migrations.

  • There might be some more useful information in the traceback than just that one line. It may be helpful if you posted the full command with the complete traceback and all other output.

Thanks for the correction

django.core.exceptions.ImproperlyConfigured: Requested setting CSRF_TRUSTED_ORIGINS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Now I’m confused. How does this relate to the migration issue in the original post?

Sorry, to clear things up that’s the explanation on the terminal when running the above given command

Please post the complete output, including the command being issued.

(src) ➜  src git:(main) ✗ django-admin sqlmigrate app_label migration_name
Traceback (most recent call last):
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/bin/django-admin", line 8, in <module>
    sys.exit(execute_from_command_line())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/commands/sqlmigrate.py", line 38, in execute
    return super().execute(*args, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/base.py", line 453, in execute
    self.check()
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/checks/compatibility/django_4_0.py", line 9, in check_csrf_trusted_origins
    for origin in settings.CSRF_TRUSTED_ORIGINS:
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/conf/__init__.py", line 102, in __getattr__
    self._setup(name)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/conf/__init__.py", line 82, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting CSRF_TRUSTED_ORIGINS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Ok, you should be running this command using your manage.py command in the same manner as you would run makemigrations and migrate instead of using django-admin. (Yes, you can use django-admin, but then you would need to set the environment variables that manage.py sets for you.)

(src) ➜  src git:(main) ✗ python manage.py sqlmigrate orders 0014 
BEGIN;
--
-- Alter field total on cartdetail
--
CREATE TABLE "new__orders_cartdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NULL, "qauntity" integer NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_cart" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);
INSERT INTO "new__orders_cartdetail" ("id", "qauntity", "order_id", "product_id", "total") SELECT "id", "qauntity", "order_id", "product_id", "total" FROM "orders_cartdetail";
DROP TABLE "orders_cartdetail";
ALTER TABLE "new__orders_cartdetail" RENAME TO "orders_cartdetail";
CREATE INDEX "orders_cartdetail_order_id_091b0028" ON "orders_cartdetail" ("order_id");
CREATE INDEX "orders_cartdetail_product_id_40b59fd6" ON "orders_cartdetail" ("product_id");
--
-- Alter field total on order
--
CREATE TABLE "new__orders_order" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "status" varchar(12) NOT NULL, "code" varchar(20) NOT NULL, "order_time" datetime NOT NULL, "delivery_time" datetime NULL, "total_with_coupon" real NULL, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED, "delivery_address_id" bigint NULL REFERENCES "accounts_address" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NULL);
INSERT INTO "new__orders_order" ("id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total") SELECT "id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total" FROM "orders_order";
DROP TABLE "orders_order";
ALTER TABLE "new__orders_order" RENAME TO "orders_order";
CREATE INDEX "orders_order_coupon_id_5bddb887" ON "orders_order" ("coupon_id");
CREATE INDEX "orders_order_delivery_address_id_6ecfdbf4" ON "orders_order" ("delivery_address_id");
CREATE INDEX "orders_order_user_id_e9b59eb1" ON "orders_order" ("user_id");
--
-- Alter field total on orderdetail
--
CREATE TABLE "new__orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "qauntity" integer NOT NULL, "price" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NULL);
INSERT INTO "new__orders_orderdetail" ("id", "qauntity", "price", "order_id", "product_id", "total") SELECT "id", "qauntity", "price", "order_id", "product_id", "total" FROM "orders_orderdetail";
DROP TABLE "orders_orderdetail";
ALTER TABLE "new__orders_orderdetail" RENAME TO "orders_orderdetail";
CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");
CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");
COMMIT;

from the latest migration

Is this the migration that is failing? Please post the complete output from your migrate command.

All of the previous ones including this latest one

**AmazoneClone** **git:(****main****)** **✗** cd src

**➜** **src** **git:(****main****)** **✗** pipenv shell

Launching subshell in virtual environment...

. /Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/bin/activate

**➜** **src** **git:(****main****)** **✗** . /Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/bin/activate

(src) **➜** **src** **git:(****main****)** **✗** code .

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0001

BEGIN;

--

-- Create model Coupon

--

CREATE TABLE "orders_coupon" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "code" varchar(20) NOT NULL, "start_date" date NOT NULL, "end_date" date NOT NULL, "quantity" integer NOT NULL, "discount" real NOT NULL);

--

-- Create model Cart

--

CREATE TABLE "orders_cart" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "status" varchar(12) NOT NULL, "total_with_coupon" real NULL, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED);

--

-- Create model CartDetail

--

CREATE TABLE "orders_cartdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "qauntity" integer NOT NULL, "total" real NULL, "order_id" bigint NOT NULL REFERENCES "orders_cart" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

--

-- Create model Order

--

CREATE TABLE "orders_order" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "status" varchar(12) NOT NULL, "code" varchar(20) NOT NULL, "order_time" datetime NOT NULL, "delivery_time" datetime NULL, "total" real NOT NULL, "total_with_coupon" real NULL, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED, "delivery_address_id" bigint NULL REFERENCES "accounts_address" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);

--

-- Create model OrderDetail

--

CREATE TABLE "orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "qauntity" integer NOT NULL, "price" real NOT NULL, "total" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

CREATE INDEX "orders_cart_user_id_121a069e" ON "orders_cart" ("user_id");

CREATE INDEX "orders_cart_coupon_id_1516a12b" ON "orders_cart" ("coupon_id");

CREATE INDEX "orders_cartdetail_order_id_091b0028" ON "orders_cartdetail" ("order_id");

CREATE INDEX "orders_cartdetail_product_id_40b59fd6" ON "orders_cartdetail" ("product_id");

CREATE INDEX "orders_order_coupon_id_5bddb887" ON "orders_order" ("coupon_id");

CREATE INDEX "orders_order_delivery_address_id_6ecfdbf4" ON "orders_order" ("delivery_address_id");

CREATE INDEX "orders_order_user_id_e9b59eb1" ON "orders_order" ("user_id");

CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");

CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0002

BEGIN;

--

-- Alter field total on order

--

CREATE TABLE "new__orders_order" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NULL, "status" varchar(12) NOT NULL, "code" varchar(20) NOT NULL, "order_time" datetime NOT NULL, "delivery_time" datetime NULL, "total_with_coupon" real NULL, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED, "delivery_address_id" bigint NULL REFERENCES "accounts_address" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_order" ("id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total") SELECT "id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total" FROM "orders_order";

DROP TABLE "orders_order";

ALTER TABLE "new__orders_order" RENAME TO "orders_order";

CREATE INDEX "orders_order_coupon_id_5bddb887" ON "orders_order" ("coupon_id");

CREATE INDEX "orders_order_delivery_address_id_6ecfdbf4" ON "orders_order" ("delivery_address_id");

CREATE INDEX "orders_order_user_id_e9b59eb1" ON "orders_order" ("user_id");

--

-- Alter field total on orderdetail

--

CREATE TABLE "new__orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "qauntity" integer NOT NULL, "price" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NULL);

INSERT INTO "new__orders_orderdetail" ("id", "qauntity", "price", "order_id", "product_id", "total") SELECT "id", "qauntity", "price", "order_id", "product_id", "total" FROM "orders_orderdetail";

DROP TABLE "orders_orderdetail";

ALTER TABLE "new__orders_orderdetail" RENAME TO "orders_orderdetail";

CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");

CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0003

BEGIN;

--

-- Alter field end_date on coupon

--

CREATE TABLE "new__orders_coupon" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "end_date" date NULL, "code" varchar(20) NOT NULL, "start_date" date NOT NULL, "quantity" integer NOT NULL, "discount" real NOT NULL);

INSERT INTO "new__orders_coupon" ("id", "code", "start_date", "quantity", "discount", "end_date") SELECT "id", "code", "start_date", "quantity", "discount", "end_date" FROM "orders_coupon";

DROP TABLE "orders_coupon";

ALTER TABLE "new__orders_coupon" RENAME TO "orders_coupon";

--

-- Alter field total on order

--

CREATE TABLE "new__orders_order" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "status" varchar(12) NOT NULL, "code" varchar(20) NOT NULL, "order_time" datetime NOT NULL, "delivery_time" datetime NULL, "total_with_coupon" real NULL, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED, "delivery_address_id" bigint NULL REFERENCES "accounts_address" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NOT NULL);

INSERT INTO "new__orders_order" ("id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total") SELECT "id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", coalesce("total", NULL) FROM "orders_order";

DROP TABLE "orders_order";

ALTER TABLE "new__orders_order" RENAME TO "orders_order";

CREATE INDEX "orders_order_coupon_id_5bddb887" ON "orders_order" ("coupon_id");

CREATE INDEX "orders_order_delivery_address_id_6ecfdbf4" ON "orders_order" ("delivery_address_id");

CREATE INDEX "orders_order_user_id_e9b59eb1" ON "orders_order" ("user_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0004

BEGIN;

--

-- Alter field end_date on coupon

--

CREATE TABLE "new__orders_coupon" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "end_date" date NULL, "code" varchar(20) NOT NULL, "start_date" date NOT NULL, "quantity" integer NOT NULL, "discount" real NOT NULL);

INSERT INTO "new__orders_coupon" ("id", "code", "start_date", "quantity", "discount", "end_date") SELECT "id", "code", "start_date", "quantity", "discount", "end_date" FROM "orders_coupon";

DROP TABLE "orders_coupon";

ALTER TABLE "new__orders_coupon" RENAME TO "orders_coupon";

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0005

BEGIN;

--

-- Alter field end_date on coupon

--

CREATE TABLE "new__orders_coupon" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "end_date" date NULL, "code" varchar(20) NOT NULL, "start_date" date NOT NULL, "quantity" integer NOT NULL, "discount" real NOT NULL);

INSERT INTO "new__orders_coupon" ("id", "code", "start_date", "quantity", "discount", "end_date") SELECT "id", "code", "start_date", "quantity", "discount", "end_date" FROM "orders_coupon";

DROP TABLE "orders_coupon";

ALTER TABLE "new__orders_coupon" RENAME TO "orders_coupon";

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0006

BEGIN;

--

-- Alter field total on cartdetail

--

CREATE TABLE "new__orders_cartdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" decimal NOT NULL, "qauntity" integer NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_cart" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_cartdetail" ("id", "qauntity", "order_id", "product_id", "total") SELECT "id", "qauntity", "order_id", "product_id", coalesce("total", '0.00') FROM "orders_cartdetail";

DROP TABLE "orders_cartdetail";

ALTER TABLE "new__orders_cartdetail" RENAME TO "orders_cartdetail";

CREATE INDEX "orders_cartdetail_order_id_091b0028" ON "orders_cartdetail" ("order_id");

CREATE INDEX "orders_cartdetail_product_id_40b59fd6" ON "orders_cartdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0007

BEGIN;

--

-- Alter field total on cartdetail

--

CREATE TABLE "new__orders_cartdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NULL, "qauntity" integer NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_cart" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_cartdetail" ("id", "qauntity", "order_id", "product_id", "total") SELECT "id", "qauntity", "order_id", "product_id", "total" FROM "orders_cartdetail";

DROP TABLE "orders_cartdetail";

ALTER TABLE "new__orders_cartdetail" RENAME TO "orders_cartdetail";

CREATE INDEX "orders_cartdetail_order_id_091b0028" ON "orders_cartdetail" ("order_id");

CREATE INDEX "orders_cartdetail_product_id_40b59fd6" ON "orders_cartdetail" ("product_id");

--

-- Alter field total on order

--

CREATE TABLE "new__orders_order" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "status" varchar(12) NOT NULL, "code" varchar(20) NOT NULL, "order_time" datetime NOT NULL, "delivery_time" datetime NULL, "total_with_coupon" real NULL, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED, "delivery_address_id" bigint NULL REFERENCES "accounts_address" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NULL);

INSERT INTO "new__orders_order" ("id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total") SELECT "id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total" FROM "orders_order";

DROP TABLE "orders_order";

ALTER TABLE "new__orders_order" RENAME TO "orders_order";

CREATE INDEX "orders_order_coupon_id_5bddb887" ON "orders_order" ("coupon_id");

CREATE INDEX "orders_order_delivery_address_id_6ecfdbf4" ON "orders_order" ("delivery_address_id");

CREATE INDEX "orders_order_user_id_e9b59eb1" ON "orders_order" ("user_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0008

BEGIN;

--

-- Alter field total on orderdetail

--

CREATE TABLE "new__orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NOT NULL, "qauntity" integer NOT NULL, "price" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_orderdetail" ("id", "qauntity", "price", "order_id", "product_id", "total") SELECT "id", "qauntity", "price", "order_id", "product_id", coalesce("total", 1.0) FROM "orders_orderdetail";

DROP TABLE "orders_orderdetail";

ALTER TABLE "new__orders_orderdetail" RENAME TO "orders_orderdetail";

CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");

CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0009

BEGIN;

--

-- Alter field total on orderdetail

--

CREATE TABLE "new__orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NULL, "qauntity" integer NOT NULL, "price" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_orderdetail" ("id", "qauntity", "price", "order_id", "product_id", "total") SELECT "id", "qauntity", "price", "order_id", "product_id", "total" FROM "orders_orderdetail";

DROP TABLE "orders_orderdetail";

ALTER TABLE "new__orders_orderdetail" RENAME TO "orders_orderdetail";

CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");

CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0010

BEGIN;

--

-- Alter field total on orderdetail

--

CREATE TABLE "new__orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NULL, "qauntity" integer NOT NULL, "price" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_orderdetail" ("id", "qauntity", "price", "order_id", "product_id", "total") SELECT "id", "qauntity", "price", "order_id", "product_id", "total" FROM "orders_orderdetail";

DROP TABLE "orders_orderdetail";

ALTER TABLE "new__orders_orderdetail" RENAME TO "orders_orderdetail";

CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");

CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0011

BEGIN;

--

-- Alter field end_date on coupon

--

CREATE TABLE "new__orders_coupon" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "end_date" date NULL, "code" varchar(20) NOT NULL, "start_date" date NOT NULL, "quantity" integer NOT NULL, "discount" real NOT NULL);

INSERT INTO "new__orders_coupon" ("id", "code", "start_date", "quantity", "discount", "end_date") SELECT "id", "code", "start_date", "quantity", "discount", "end_date" FROM "orders_coupon";

DROP TABLE "orders_coupon";

ALTER TABLE "new__orders_coupon" RENAME TO "orders_coupon";

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0012

BEGIN;

--

-- Alter field total on order

--

CREATE TABLE "new__orders_order" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NOT NULL, "status" varchar(12) NOT NULL, "code" varchar(20) NOT NULL, "order_time" datetime NOT NULL, "delivery_time" datetime NULL, "total_with_coupon" real NULL, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED, "delivery_address_id" bigint NULL REFERENCES "accounts_address" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_order" ("id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total") SELECT "id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", coalesce("total", 0.0) FROM "orders_order";

DROP TABLE "orders_order";

ALTER TABLE "new__orders_order" RENAME TO "orders_order";

CREATE INDEX "orders_order_coupon_id_5bddb887" ON "orders_order" ("coupon_id");

CREATE INDEX "orders_order_delivery_address_id_6ecfdbf4" ON "orders_order" ("delivery_address_id");

CREATE INDEX "orders_order_user_id_e9b59eb1" ON "orders_order" ("user_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0013

BEGIN;

--

-- Alter field total on cartdetail

--

CREATE TABLE "new__orders_cartdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NOT NULL, "qauntity" integer NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_cart" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_cartdetail" ("id", "qauntity", "order_id", "product_id", "total") SELECT "id", "qauntity", "order_id", "product_id", coalesce("total", 0.0) FROM "orders_cartdetail";

DROP TABLE "orders_cartdetail";

ALTER TABLE "new__orders_cartdetail" RENAME TO "orders_cartdetail";

CREATE INDEX "orders_cartdetail_order_id_091b0028" ON "orders_cartdetail" ("order_id");

CREATE INDEX "orders_cartdetail_product_id_40b59fd6" ON "orders_cartdetail" ("product_id");

--

-- Alter field total on orderdetail

--

CREATE TABLE "new__orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "qauntity" integer NOT NULL, "price" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NOT NULL);

INSERT INTO "new__orders_orderdetail" ("id", "qauntity", "price", "order_id", "product_id", "total") SELECT "id", "qauntity", "price", "order_id", "product_id", coalesce("total", 0.0) FROM "orders_orderdetail";

DROP TABLE "orders_orderdetail";

ALTER TABLE "new__orders_orderdetail" RENAME TO "orders_orderdetail";

CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");

CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗** python manage.py sqlmigrate orders 0014

BEGIN;

--

-- Alter field total on cartdetail

--

CREATE TABLE "new__orders_cartdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "total" real NULL, "qauntity" integer NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_cart" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED);

INSERT INTO "new__orders_cartdetail" ("id", "qauntity", "order_id", "product_id", "total") SELECT "id", "qauntity", "order_id", "product_id", "total" FROM "orders_cartdetail";

DROP TABLE "orders_cartdetail";

ALTER TABLE "new__orders_cartdetail" RENAME TO "orders_cartdetail";

CREATE INDEX "orders_cartdetail_order_id_091b0028" ON "orders_cartdetail" ("order_id");

CREATE INDEX "orders_cartdetail_product_id_40b59fd6" ON "orders_cartdetail" ("product_id");

--

-- Alter field total on order

--

CREATE TABLE "new__orders_order" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "status" varchar(12) NOT NULL, "code" varchar(20) NOT NULL, "order_time" datetime NOT NULL, "delivery_time" datetime NULL, "total_with_coupon" real NULL, "coupon_id" bigint NULL REFERENCES "orders_coupon" ("id") DEFERRABLE INITIALLY DEFERRED, "delivery_address_id" bigint NULL REFERENCES "accounts_address" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NULL);

INSERT INTO "new__orders_order" ("id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total") SELECT "id", "status", "code", "order_time", "delivery_time", "total_with_coupon", "coupon_id", "delivery_address_id", "user_id", "total" FROM "orders_order";

DROP TABLE "orders_order";

ALTER TABLE "new__orders_order" RENAME TO "orders_order";

CREATE INDEX "orders_order_coupon_id_5bddb887" ON "orders_order" ("coupon_id");

CREATE INDEX "orders_order_delivery_address_id_6ecfdbf4" ON "orders_order" ("delivery_address_id");

CREATE INDEX "orders_order_user_id_e9b59eb1" ON "orders_order" ("user_id");

--

-- Alter field total on orderdetail

--

CREATE TABLE "new__orders_orderdetail" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "qauntity" integer NOT NULL, "price" real NOT NULL, "order_id" bigint NOT NULL REFERENCES "orders_order" ("id") DEFERRABLE INITIALLY DEFERRED, "product_id" bigint NULL REFERENCES "products_product" ("id") DEFERRABLE INITIALLY DEFERRED, "total" real NULL);

INSERT INTO "new__orders_orderdetail" ("id", "qauntity", "price", "order_id", "product_id", "total") SELECT "id", "qauntity", "price", "order_id", "product_id", "total" FROM "orders_orderdetail";

DROP TABLE "orders_orderdetail";

ALTER TABLE "new__orders_orderdetail" RENAME TO "orders_orderdetail";

CREATE INDEX "orders_orderdetail_order_id_8d02dd1f" ON "orders_orderdetail" ("order_id");

CREATE INDEX "orders_orderdetail_product_id_3ecd225e" ON "orders_orderdetail" ("product_id");

COMMIT;

(src) **➜** **src** **git:(****main****)** **✗**

That’s output from a sqlmigrate command, not a migrate command.

(src) ➜  src git:(main) ✗ python manage.py migrate               
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, orders, products, sessions, settings, taggit
Running migrations:
  Applying orders.0003_alter_coupon_end_date_alter_order_total...Traceback (most recent call last):
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.IntegrityError: NOT NULL constraint failed: new__orders_order.total

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/abdulmohsenal-sayegh/Desktop/Vs code/Amzn/AmazoneClone/src/manage.py", line 22, in <module>
    main()
  File "/Users/abdulmohsenal-sayegh/Desktop/Vs code/Amzn/AmazoneClone/src/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/migrations/operations/fields.py", line 235, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/sqlite3/schema.py", line 173, in alter_field
    super().alter_field(model, old_field, new_field, strict=strict)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/base/schema.py", line 830, in alter_field
    self._alter_field(
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/sqlite3/schema.py", line 461, in _alter_field
    self._remake_table(model, alter_fields=[(old_field, new_field)])
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/sqlite3/schema.py", line 334, in _remake_table
    self.execute(
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/base/schema.py", line 201, in execute
    cursor.execute(sql, params)
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abdulmohsenal-sayegh/.local/share/virtualenvs/src-A9O8BnbG/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.IntegrityError: NOT NULL constraint failed: new__orders_order.total
(src) ➜  src git:(main) ✗

So this is the migration that is failing, and this would be the migration to focus on.

Do you have any migrations in other apps that would be dependent on this one?

Do you have other deployment environments where this migration has already been applied?

If the answer to both of those questions is “No”, then the easiest thing to do would be to just delete all migrations from 0003 on. Although I find it interesting that you’ve gone from migration 0003 to 0014 without applying these migrations and previously encountering this issue.

If it’s a case where you’ve been working in a development environment and are now trying to deploy this to a different database, then you have a different issue to address. You may need to manually edit that migration file to change that field to allow nulls in that migration.

Do you have any migrations in other apps that would be dependent on this one?
Yes
Do you have other deployment environments where this migration has already been applied?
No
You may need to manually edit that migration file to change that field to allow nulls in that migration.
How? and what do I need to modify in the migration itself?

It’s python code, just like pretty much everything else in Django.

See the docs for Migration files

You would need to post the migration file here for us to be able to answer that question.