[django] django no such table:

I have models in Django set up as below.

class Pupil(models.Model):
    forename = models.CharField(max_length=30)
    surname = models.CharField(max_length=30)
    dateofbirth = models.DateField()
    year = models.IntegerField()
    class_group = models.CharField(max_length=30)
    email = models.EmailField(blank=True)
    assignments = models.ManyToManyField('Assignment', verbose_name='related assignments')

    def __unicode__(self):
        return u'%s %s' % (self.forename, self.surname)

class Subject(models.Model):
    name = models.CharField(max_length=30)
    level = models.CharField(max_length=30)
    teachers = models.ManyToManyField('Teacher', verbose_name='related teachers')

    def __unicode__(self):
        return self.name

class Teacher(models.Model):
    forename = models.CharField(max_length=30)
    surname = models.CharField(max_length=30)
    email = models.EmailField(blank=True)

    def __unicode__(self):
        return u'%s %s' % (self.forename, self.surname)

class Assignment(models.Model):
    assignment_name = models.CharField(max_length=30)
    date_assigned = models.DateField()
    date_submitted = models.DateField()

    def __unicode__(self):
        return self.assignment_name

When I attempt to add a pupil and attach an assignment to the pupil in the admin, I get a database error -

no such table: homework_pupil_assignments

after reading this I realised this could be due to django not updating changes to my models as when I do manage.py sqlall homework

I see the following:

BEGIN;
CREATE TABLE "homework_pupil_assignments" (
    "id" integer NOT NULL PRIMARY KEY,
    "pupil_id" integer NOT NULL,
    "assignment_id" integer NOT NULL,
    UNIQUE ("pupil_id", "assignment_id")
)
;
CREATE TABLE "homework_pupil" (
    "id" integer NOT NULL PRIMARY KEY,
    "forename" varchar(30) NOT NULL,
    "surname" varchar(30) NOT NULL,
    "dateofbirth" date NOT NULL,
    "year" integer NOT NULL,
    "class_group" varchar(30) NOT NULL,
    "email" varchar(75) NOT NULL
)
;
CREATE TABLE "homework_subject_teachers" (
    "id" integer NOT NULL PRIMARY KEY,
    "subject_id" integer NOT NULL,
    "teacher_id" integer NOT NULL,
    UNIQUE ("subject_id", "teacher_id")
)
;
CREATE TABLE "homework_subject" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(30) NOT NULL,
    "level" varchar(30) NOT NULL
)
;
CREATE TABLE "homework_teacher" (
    "id" integer NOT NULL PRIMARY KEY,
    "forename" varchar(30) NOT NULL,
    "surname" varchar(30) NOT NULL,
    "email" varchar(75) NOT NULL
)
;
CREATE TABLE "homework_assignment" (
    "id" integer NOT NULL PRIMARY KEY,
    "assignment_name" varchar(30) NOT NULL,
    "date_assigned" date NOT NULL,
    "date_submitted" date NOT NULL
)
;
COMMIT;

I then installed South, following the directions to get up and running with an existing app, in the hope of successfully syncing these tables. No joy.

Can anyone suggest how I can get the database (sqlite3) to reflect the models or point out what I'm doing wrong?

This question is related to django django-south

The answer is


sqlall just prints the SQL, it doesn't execute it. syncdb will create tables that aren't already created, but it won't modify existing tables.


I think maybe you can try

python manage.py syncdb

Even sqlite3 need syncdb

sencondly, you can check your sql file name

it should look like xxx.s3db


. first step delete db.sqlite3 file . go to terminal and run commands:

  • python manage.py makemigrations
  • python manage.py migrate
  • python manage.py createsuperuser
  • python manage.py runserver . go to admin page every thing ok now.

You can try this!

python manage.py migrate --run-syncdb

I have the same problem with Django 1.9 and 1.10. This code works!


One way to sync your database to your django models is to delete your database file and run makemigrations and migrate commands again. This will reflect your django models structure to your database from scratch. Although, make sure to backup your database file before deleting in case you need your records.

This solution worked for me since I wasn't much bothered about the data and just wanted my db and models structure to sync up.


If you are using latest version of django 2.x or 1.11.x then you have to first create migrations ,

python manage.py makemigrations

After that you just have to run migrate command for syncing database .

python manage.py migrate --run-syncdb

These will sync your database and python models and also second command will print all sql behind it.