我在heroku上使用peewee,但根据peewee文档,删除相关表必须在代码中完成。psycopg2 psycopg2.InternalError依赖对象
问题是怎么回事?
我有以下型号
包括PostgreSQL的自定义语句DROP TABLE方法:
def drop_tables():
WebPage.drop_table(fail_silently=True)
Term.drop_table(fail_silently=True)
WebPageTerms.drop_table(fail_silently=True)
作为部署的一部分,我把所有的表,为了获得新创建它们字段添加。
我曾尝试以下方法,但它仍然没有工作:
conn = psycopg2.connect("dbname='' user='' host='' password=''")
curr = conn.cursor()
curr.execute("DROP table webpageterms CASCADE")
curr.execute("DROP table webpage CASCADE")
curr.close()
conn.close()
,但我不断收到同样的错误
psycopg2.InternalError: cannot drop table webpage because other objects depend on it DETAIL: constraint webpageterms_webpage_id_fkey on table webpageterms depends on table webpage HINT: Use DROP ... CASCADE to drop the dependent objects too.
我怎么可以删除这些表?
**编辑** 试图将代码更改为:
curr.execute("ALTER TABLE webpageterms DROP CONSTRAINT webpageterms_webpage_id_fkey")
,但我得到:
psycopg2.InternalError: cannot drop table webpage because other objects depend on it
DETAIL: constraint webpageterms_webpage_id_fkey on table webpageterms depends on table webpage
HINT: Use DROP ... CASCADE to drop the dependent objects too.
删除约束可以解决问题,但通过以不同的顺序删除它们可以更轻松地解决问题。 – coleifer 2013-04-30 00:30:42