I have postgres database, I my application is build on using django
and I used south migration
to maintain the database schema. I have following scenario:
user_table
has a foreign key relationship with userclickstream_stream
and userclickstream_click
has a foreign key relationship with user_stream_table.
I want to delete all the records in userclickstream_stream
and userclickstream_click
. But I don't want to delete any record in the user_table. What is the best way to accomplish this?
Here is how my user_stream_table looks like:
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
Here is how the 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
It would be great if there is a good SQL way to take care of this rather than going the south migration route. If not I was thinking to do following:
I was thinking simply delete the record in south migration history table and re-build the schema using south I am not really sure if thats the right way to go. But to do that I need to drop the these two tables first. I might not be able to drop the table due to the foreign key relationship.
Lets say I drop it then I might be able to do the following since south migration history table doesn't have any record of these two tables.
./manage.py schemamigration userclickstream --initial
./manage.py migrate userclickstream