From the beginning, I created the tables in my myapp folder and from the shell ran
python manage.py sql myapp
which then produced the expected output
>
BEGIN;
CREATE TABLE "myapp_dude" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(128) NOT NULL
)
;
CREATE TABLE "myapp_group_members" (
"id" integer NOT NULL PRIMARY KEY,
"group_id" integer NOT NULL,
"dude_id" integer NOT NULL REFERENCES "myapp_dude" ("id"),
UNIQUE ("group_id", "dude_id")
)
;
CREATE TABLE "myapp_group" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(128) NOT NULL
)
;
CREATE TABLE "myapp_membership" (
"id" integer NOT NULL PRIMARY KEY,
"dude_id" integer NOT NULL REFERENCES "myapp_dude" ("id"),
"group_id" integer NOT NULL REFERENCES "myapp_group" ("id"),
"date_joined" date NOT NULL,
"invite_reason" varchar(64) NOT NULL
)
;
COMMIT;
I then synced the databases and began running the python shell. It accepts all arguments into my tables and adds the names/groups appropriately, even the membership works, however, when I try to simply save it, I get the following error.
>>> from myapp.models import Membership, Group, Dude
>>> from datetime import date
>>> ringo = Dude.objects.create(name="Ringo Starr")
>>> paul = Dude.objects.create(name="Paul McCartney")
>>> beatles = Group.objects.create(name="The Beatles")
>>> m1 = Membership(dude=ringo, group=beatles,
... date_joined=date(1962, 8, 16),
... invite_reason="Needed a new drummer.")
>>> m1.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 460, in sa
ve
self.save_base(using=using, force_insert=force_insert, force_update=force_up
date)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 553, in sa
ve_base
result = manager._insert(values, return_id=update_pk, using=using)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 195, in
_insert
return insert_query(self.model, values, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1436, in
insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 79
1, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 73
5, in execute_sql
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 34, in e
xecute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
234, in execute
return Database.Cursor.execute(self, query, params)
DatabaseError: table myapp_membership has no column named dude_id
I am completely thrown for a loop with this because as you can see, when the tables are created, there IS a “dude_id” column created, but at the bottom of the error message, it says that it does not have one. And also the fact that I can add to dude, and add to the membership table fine but then cannot save doesn’t make much sense either. I have gone through this website, google, you name it but I have been unable to come up for a solution to this one.. Any insight will be much appreciated!!
The error is pretty self explanatory. Your SQL schema differs from the one thas is defined in django. Try this