Owen
Owen

Reputation: 57

Django: sqlite3.OperationalError: no such table in python shell

I'm following a django blog tutorial. When I used python manage.py shell and tried following commands in shell

from mainsite.models import Category, Tag
c = Category(name='category test')
c.save()

this error occured:

sqlite3.OperationalError: no such table: mainsite_category

This is my model.py in mainsite folder, which I think is correct:

from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
    name = models.CharField(max_length=100)

def __str__(self):
    return self.name

I have tried manage.py makemigrations, manage.py migrate and can successfully use manage.py runserver to see the website. So what is wrong here?

Edit: this is my INSTALLED APP in settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
]

And my project structure

|____djangoBlog
| |______init__.py
| |____settings.py
| |____urls.py
| |____wsgi.py
|____mainsite
| |______init__.py
| |____admin.py
| |____apps.py
| |____models.py
| |____tests.py
| |____urls.py
| |____views.py
|____manage.py
|____README.md
|____requirements.txt
|____templates
| |____blog
| | |____index.html

Edit: When I run python manage.py makemigrations and python manage.py migrate, the output is as follows:

No changes detected

Operations to perform:
    Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
    No migrations to apply.

Upvotes: 1

Views: 2948

Answers (3)

HamzDiou
HamzDiou

Reputation: 638

@Owen, According to vishes_shell's answer, you'll have to do :

python manage.py makemigrations mainsite

the first time in order to generate a folder migrations that will contain your migrations, then you can just do

python manage.py makemigrations

In the futur if you add a new app, don't forget to make again the first command (changing the "mainsite" by the name of your app) before using the second one.

Upvotes: 1

vishes_shell
vishes_shell

Reputation: 23484

UPD:

Since your project structure is lack of migrations folder in mainsite app, it means that you haven't created migrations for that app. Run python manage.py makemigrations mainsite and then python manage.py migrate.

From docs:

To add migrations to an app that doesn’t have a migrations directory, run makemigrations with the app’s app_label.

And yes, you can create model with simple instance creation. From docs Creating objects.

Original answer:

You need to be sure that you've included your 'mainsite' app in INSTALLED_APPS in settings.py and then run migrations. After that you should be able to do the following:

c = Category.objects.create(name='category test')
c.save()

Upvotes: 2

echefede
echefede

Reputation: 536

It seems like in your models.py you need an indentation at name:

class Category(models.Model):
    name = models.CharField(max_length=100)

Upvotes: 0

Related Questions