[英]Peewee circular foreign key dependencies exception
I'm trying to replicate Peewee example app using circular dependences as explained in the docs , despite the creator clearly states this is usually a bad idea. 我试图按照文档中的解释使用循环依赖来复制Peewee示例应用程序,尽管创建者明确指出这通常是个坏主意。 This is the code mainly copied from the docs:
这是主要从文档复制的代码:
from peewee import *
db = SqliteDatabase(None)
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField()
# Tweet has not been defined yet so use the deferred reference.
favorite_tweet = DeferredForeignKey('Tweet', null=True)
class Tweet(BaseModel):
message = TextField()
user = ForeignKeyField(User, backref='tweets')
db.init('twitter.db')
db.create_tables([User, Tweet])
User._schema.create_foreign_key(User.favorite_tweet) #Error
db.close()
I'm getting the an exception in the line commented with #Error. 我在用#Error注释的行中得到了一个异常。 This line is needed, as explained in the docs:
如文档中所述,这行是必需的:
When you call create_table we will again encounter the same issue.
当您调用create_table时,我们将再次遇到相同的问题。 For this reason peewee will not automatically create a foreign key constraint for any deferred foreign keys.
因此,peewee不会为任何延迟的外键自动创建外键约束。
To create the tables and the foreign-key constraint, you can use the SchemaManager.create_foreign_key() method to create the constraint after creating the tables.
要创建表和外键约束,可以在创建表后使用SchemaManager.create_foreign_key()方法创建约束。
This is the exception I get using Python 3.5.2: 这是我使用Python 3.5.2获得的例外:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2653, in execute_sql
cursor.execute(sql, params or ())
sqlite3.OperationalError: near "CONSTRAINT": syntax error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test3.py", line 23, in <module>
User._schema.create_foreign_key(User.favorite_tweet)
File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 4930, in create_foreign_key
self.database.execute(self._create_foreign_key(field))
File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2666, in execute
return self.execute_sql(sql, params, commit=commit)
File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2660, in execute_sql
self.commit()
File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2451, in __exit__ reraise(new_type, new_type(*exc_args), traceback)
File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 178, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2653, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: near "CONSTRAINT": syntax error
Sqlite does not support ALTER TABLE ADD CONSTRAINT -- so when you're using Sqlite you should omit the additional call to create_foreign_key(). Sqlite不支持ALTER TABLE ADD CONSTRAINT-因此,当您使用Sqlite时,应省略对create_foreign_key()的附加调用。
There is a clear NOTE in the docs that says: 在文档中有一个明确的注释:
Because SQLite has limited support for altering tables, foreign-key constraints cannot be added to a table after it has been created.
由于SQLite对更改表的支持有限,因此在创建表后不能将外键约束添加到表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.