2013-02-08 17 views
9

olarak değiştirme Aşağıdaki, "kapsayıcılar" adında bir tablonun bir pasajıdır.Bir sütunu dize dizisinden postgresql

 Column  |   Type    |   Modifiers    
--------------------+-----------------------------+--------------------------------- 
id     | uuid      | not null 
name    | character varying(255)  | 
products   | character varying   | default '{}'::character varying 

nasıl "character varying[]" için products sütun ve default '{}'::character varying[] için gelen düzenleyiciler değiştirebilir ? Aslında, bir dizeyi bir dize dizisine dönüştürmek istiyorum. Ürün sütununun karakter sayısı üzerinde bir sınırı olmadığını unutmayın.

alter table "containers" alter "products" type character varying[]; 

Postgres varchar[] için varchar örtük dökme yoktur aşağıdaki hatayı

ERROR: column "products" cannot be cast to type character varying[]

cevap

11

atar. Türlerin dönüştürülmesini nasıl gerçekleştireceğinizi belirtmelisiniz. USING expression maddesinde yapmalısınız (belgelerindeki ALTER TABLE'a bakın).

alter table containers alter products drop default; 
alter table containers alter products type text[] using array[products]; 
alter table containers alter products set default '{}'; 

üç operasyonlar

The USING option of SET DATA TYPE can actually specify any expression involving the old values of the row; that is, it can refer to other columns as well as the one being converted. This allows very general conversions to be done with the SET DATA TYPE syntax. Because of this flexibility, the USING expression is not applied to the column's default value (if any); the result might not be a constant expression as required for a default. This means that when there is no implicit or assignment cast from old to new type, SET DATA TYPE might fail to convert the default even though a USING clause is supplied. In such cases, drop the default with DROP DEFAULT, perform the ALTER TYPE, and then use SET DEFAULT to add a suitable new default.

bir açıklamada yapılabilir:

alter table containers 
    alter products drop default, 
    alter products type text[] using array[products], 
    alter products set default '{}'; 
o belgelerinde açıklandığı gibi, bırakın ve sütunun varsayılan değeri yeniden oluşturmak zorunda Bu durumda
+0

Teşekkürler. Yine de bu döküm hatası alınıyor - HATA: sütun "ürünleri" için varsayılan yazıyı yazmak için kullanılamaz [] – papdel

+0

işlevi oluşturmak veya değiştirmek string_to_string_array (değer karakteri değişiyor) , $ select array [$ 1] $$; ve daha sonra tablosu "kapsayıcılar" değiştirmek "ürünleri" karakterini değiştirmek için [] string_to_string_array (ürünler) karakterini değiştirir; aynı hatayı da attı. – papdel

+0

Mevcut varsayılan değeri nasıl değiştiririm? – papdel

İlgili konular