【发布时间】:2014-02-09 19:49:32
【问题描述】:
我有 postgres 数据库,我的应用程序是使用 django 构建的,我使用 south migration 来维护数据库架构。我有以下情况:
user_table 与 userclickstream_stream 具有外键关系,userclickstream_click 与 user_stream_table 具有外键关系。
我想删除userclickstream_stream 和userclickstream_click 中的所有记录。但我不想删除 user_table 中的任何记录。实现这一目标的最佳方法是什么?
这是我的 user_stream_table 的样子:
Table "public.userclickstream_stream"
Column | Type | Modifiers
-------------+--------------------------+---------------------------------------------------------------------
id | integer | not null default nextval('userclickstream_stream_id_seq'::regclass)
session_key | character varying(40) | not null
ip_address | character varying(40) | not null
referrer | text |
create_date | timestamp with time zone | not null
last_update | timestamp with time zone | not null
user_id | integer |
Indexes:
"userclickstream_stream_pkey" PRIMARY KEY, btree (id)
"userclickstream_stream_session_key_key" UNIQUE CONSTRAINT, btree (session_key)
"userclickstream_stream_user_id" btree (user_id)
Foreign-key constraints:
"user_id_refs_id_773d100c" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
Referenced by:
TABLE "userclickstream_click" CONSTRAINT "stream_id_refs_id_4c08df60" FOREIGN KEY (stream_id) REFERENCES userclickstream_stream(id) DEFERRABLE INITIALLY DEFERRED
这是 User_click_table 的方式
Table "public.userclickstream_click"
Column | Type | Modifiers
-------------+--------------------------+--------------------------------------------------------------------
id | integer | not null default nextval('userclickstream_click_id_seq'::regclass)
stream_id | integer | not null
url | text | not null
path | text | not null
create_date | timestamp with time zone | not null
Indexes:
"userclickstream_click_pkey" PRIMARY KEY, btree (id)
"userclickstream_click_stream_id" btree (stream_id)
Foreign-key constraints:
"stream_id_refs_id_4c08df60" FOREIGN KEY (stream_id) REFERENCES userclickstream_stream(id) DEFERRABLE INITIALLY DEFERRED
如果有一个很好的 SQL 方法来解决这个问题,而不是走南迁移路线,那就太好了。如果不是,我正在考虑执行以下操作:
我在想只是删除南迁移历史表中的记录并使用南重新构建架构我不确定这是否是正确的方法。但要做到这一点,我需要先删除这两个表。由于外键关系,我可能无法删除表。
假设我放弃它,那么我可能能够执行以下操作,因为南迁移历史表没有这两个表的任何记录。
./manage.py schemamigration userclickstream --initial
./manage.py migrate userclickstream
【问题讨论】:
-
您对表格的描述与表格名称不匹配。请更正一下。
-
@MikeSherrill'Catcall' 完成,谢谢,
-
除了级联删除之外,您真的需要做任何事情吗? Django syntax
标签: sql database django postgresql django-south