简体   繁体   English

在Python shell中添加多对多关系后使用.save()时出错

[英]Error when using .save() after adding a manytomany relationship in Python shell

From the beginning, I created the tables in my myapp folder and from the shell ran 从一开始,我就在myapp文件夹中创建了表,然后从Shell运行

python manage.py sql myapp 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. 然后,我同步了数据库并开始运行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. 我对此完全陷入了循环,因为如您所见,在创建表时,创建了一个“ dude_id”列,但是在错误消息的底部,它说它没有一个。 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. 而且我可以添加到dude,然后可以添加到成员资格表中但不能保存的事实也没有太大意义。 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!! 我已经浏览过这个网站,谷歌,给你起名字,但是我一直无法提出解决这个问题的方法。任何见解将不胜感激!

Can you make sure that your database has dude_id , because I think it doesn't. 您能否确定数据库具有dude_id ,因为我认为没有。 This may happen because syncdb does not recreate tables if they already exist. 这可能是因为syncdb不会重新创建表(如果它们已经存在)。

The error is pretty self explanatory. 该错误很容易解释。 Your SQL schema differs from the one thas is defined in django. 您的SQL模式与django中定义的不同。 Try this 尝试这个

python manage.py reset myapp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM