我是PostgreSQL裏的新手,只知道關於數據庫的基本操作。我有一個名爲neuro_db的數據庫,其用戶名爲neuro_user。現在我發現我無法從neuro_db中刪除表中的列。無法在PostgreSQL中刪除列
在我嘗試在ubuntu 14.04中使用psql neuro_db
之後,我進入了一個以neuro_db=#
開頭的命令行。該\d
命令會顯示這樣的表:
List of relations
Schema | Name | Type | Owner
--------+-----------------------------------+----------+------------
public | auth_group | table | neuro_user
public | auth_group_id_seq | sequence | neuro_user
public | auth_group_permissions | table | neuro_user
public | auth_group_permissions_id_seq | sequence | neuro_user
public | auth_permission | table | neuro_user
public | auth_permission_id_seq | sequence | neuro_user
public | auth_user | table | neuro_user
public | auth_user_groups | table | neuro_user
public | auth_user_groups_id_seq | sequence | neuro_user
public | auth_user_id_seq | sequence | neuro_user
public | auth_user_user_permissions | table | neuro_user
public | auth_user_user_permissions_id_seq | sequence | neuro_user
public | django_admin_log | table | neuro_user
public | django_admin_log_id_seq | sequence | neuro_user
public | django_content_type | table | neuro_user
public | django_content_type_id_seq | sequence | neuro_user
public | django_migrations | table | neuro_user
public | django_migrations_id_seq | sequence | neuro_user
public | django_session | table | neuro_user
public | neuro_category | table | neuro_user
public | neuro_category_id_seq | sequence | neuro_user
public | neuro_page | table | neuro_user
public | neuro_page_id_seq | sequence | neuro_user
(23 rows)
我想編輯表「neuro_page」並鍵入\d neuro_page
後,它會顯示此表:
Table "public.neuro_page"
Column | Type | Modifiers
-------------+------------------------+---------------------------------------------------------
id | integer | not null default nextval('neuro_page_id_seq'::regclass)
category_id | integer | not null
title | character varying(128) | not null
text | text | not null
url | character varying(200) | not null
photo | character varying(100) | not null
order | character varying(128) | not null
date | date | not null
views | integer | not null
likes | integer | not null
我想刪除「訂單「列,所以我嘗試在neuro_db=#
行後輸入ALTER TABLE neuro_page DROP COLUMN order
,但沒有任何發生。分別嘗試ALTER TABLE neuro_page
和DROP COLUMN order
後也沒有任何結果。 「訂單」欄仍然存在!並沒有錯誤信息!
我也搜索了谷歌的答案,但沒有發現任何東西。我認爲我遵循數據庫中的正常操作,但不能刪除表中的單個列。 「訂單」欄沒有外鍵。
如果有人能幫助我解決這個問題,我會非常感激。謝謝!
如果你使用Django 1.7+,你應該使用遷移,所以你不應該手動刪除列。如果手動刪除列,則可能會導致模型和遷移與數據庫不同步。 – Alasdair