2017-05-24 32 views
2

我將我的Django網站數據庫從MySQL遷移到PostgreSQL使用第三方工具pgloader。但由於MySQL默認存儲BOOLEAN數據爲TINYINT,所以在PostgreSQL中它被轉換爲SMALLINT。現在Django顯示錯誤,smallint被視爲布爾值。由於這個原因,我想將所有smallint列轉換爲布爾值。是否有單個命令將單個數據庫中所有表中的所有列轉換爲所需的類型?如果我必須爲每張桌子單獨執行操作,那也行。如何將PostgreSQL數據庫中的所有SMALLINT列轉換爲BOOLEAN?

編輯:

柱結構:

public | smallint | default '1'::smallint

這裏公共是列名,SMALLINT它的類型,有它的 '1'默認值。

我使用的代碼:

[email protected]:~$ psql -d thakurani -U utkarsh 
Password for user utkarsh: 
psql (9.6.3) 
Type "help" for help. 

thakurani=# alter table topics_topic alter COLUMN public type boolean using(public::text::boolean);        ERROR: default for column "public" cannot be cast automatically to type boolean 
thakurani=# alter table topics_topic alter COLUMN public type text using(public::text); 
ALTER TABLE 
thakurani=# alter table topics_topic alter COLUMN public type boolean using(public::boolean); 
ERROR: default for column "public" cannot be cast automatically to type boolean 
thakurani=# ALTER TABLE topics_topic ALTER COLUMN public TYPE boolean USING CASE WHEN public = '0' THEN FALSE WHEN public = '1' THEN TRUE END; 
ERROR: default for column "public" cannot be cast automatically to type boolean 
thakurani=# 
+0

你試圖只需更改模型中的類型並讓'migrations'完成這項工作? –

回答

2

你需要加倍投 - 第一投INT爲文本,然後點擊文本布爾

這裏有一個例子:

t=# create table s181(i smallint default 1::smallint); 
CREATE TABLE 
t=# insert into s181 values (0),(1); 
INSERT 0 2 
t=# alter table s181 alter COLUMN i drop default; 
ALTER TABLE 
t=# alter table s181 alter COLUMN i type boolean using(i::text::boolean); 
ALTER TABLE 
t=# alter table s181 alter COLUMN i set default true; 
ALTER TABLE 
t=# \d s181 
     Table "public.s181" 
Column | Type | Modifiers 
--------+---------+-------------- 
i  | boolean | default true 
+0

這是給我一個錯誤:錯誤:列「公共」的默認值不能自動轉換爲鍵入布爾型 –

+0

我能夠轉換爲文本類型,但從文本轉換爲布爾值給我與以前相同的錯誤。 –

+0

請使用現有的表格結構和確切的命令更新您的文章。 –

相關問題