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?
OperationalError (documentation) is one of many possible Python exceptions thrown by the Django web framework when there is an issue that occurs in the Django ORM. Note that OperationalError can be imported either from django. db or django.
south
plugin:Like T.T suggested in his answer, my previous answer was for south
migration plugin, when Django hasn't any schema migration features. Now (works in Django 1.9+):
T.T wrote:
You can try this!
python manage.py makemigrations python manage.py migrate --run-syncdb
south
migrations pluginAs I can see you done it all in wrong order, to fix it up your should complete this checklist (I assume you can't delete sqlite3 database file to start over):
- Grab any SQLite GUI tool (i.e. http://sqliteadmin.orbmu2k.de/)
- Change your model definition to match database definition (best approach is to comment new fields)
- Delete
migrations
folder in your model- Delete rows in
south_migrationhistory
table whereapp_name
match your application name (probablyhomework
)- Invoke:
./manage.py schemamigration <app_name> --initial
- Create tables by
./manage.py migrate <app_name> --fake
(--fake
will skip SQL execute because table already exists in your database)- Make changes to your app's model
- Invoke
./manage.py schemamigration <app_name> --auto
- Then apply changes to database:
./manage.py migrate <app_name>
Steps 7,8,9 repeat whenever your model needs any changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With