userx
userx

Reputation: 21

How to solve "OperationalError: no such column" in Django?

I'm making a cart app for an e-commerce site to handle users sessions. I'm getting this error in the admin page when clicking on the carts section:

OperationalError at /admin/carts/cart/ no such column: carts_cart.user_id
Request Method: GET
Request URL:    127.0.0.1:8000/admin/carts/cart

Here's the cart model:

from django.db import models
from django.conf import settings
from django.urls import reverse
from products.models import product 

user=settings.AUTH_USER_MODEL

class cart(models.Model):
    user        = models.ForeignKey(user, null=True, blank=True)
    products    = models.ManyToManyField(product, blank=True)
    total       = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
    updated     = models.DateTimeField(auto_now=True)
    timestamp   = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.id)

    def __unicode__(self):
        return str(self.id) 

views.py

from django.shortcuts import render
from .models import cart

def cart_home(request):
    return render(request,"carts/home.html",{})

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #ourapps

    'products',
    'search',
    'carts',
    'tags',
] 

products ,search ,tags works fine

Upvotes: 2

Views: 15982

Answers (4)

N Jedidiah
N Jedidiah

Reputation: 1803

I simply went to the directory of my project and manually deleted the dbsqlite file. After, i rerun...

python manage.py makemigrations

python manage.py migrate

And everything worked well

Upvotes: 6

Balepur
Balepur

Reputation: 168

I realized that I encountered this error when I forgot to do the following steps when I made changes to my model (in the models.py)

python manage.py makemigrations
python manage.py migrate

I am using Django 2.2, and it worked without problems thereafter

Upvotes: 0

Amiga85
Amiga85

Reputation: 46

Like @bharat bhushan I used

python3 manage.py migrate --fake 'app name' zero
python3 manage.py migrate 'app name'

BUT, first I had to manually open the sql browser and delete the tables for that app, as otherwise I would get a: OperationalError: table "appname_classname" already exists

So, delete the tables manually and then use the two mentioned commands.

Upvotes: 3

bharat bhushan
bharat bhushan

Reputation: 140

Just use
python3 manage.py migrate --fake 'app name' zero
python3 manage.py migrate 'app name'
to proper sync of migrations again

Upvotes: 0

Related Questions