Login

Changing field type in production

Author:
lawgon
Posted:
April 17, 2007
Language:
SQL
Version:
Not specified
Tags:
schema_evolution
Score:
2 (after 2 ratings)

Assume a model called 'Child' with a field called 'schoolstd' whic h is of integer type and not null. You need to change it to char type and not null and the same time preserve the data. The above snippet does this in postgresql.

1
2
3
4
5
6
7
begin;
alter table web_child add column strnum varchar(10);
update web_child set strnum = to_char(schoolstd,'999');
alter table web_child alter column strnum set not null;
alter table web_child drop column schoolstd;
alter table web_child rename column strnum to schoolstd;
commit;

More like this

  1. django-admin custom filter: IS NULL/IS NOT NULL by Eloff 5 years, 1 month ago
  2. Simple solution for model schema evolution / database changelog by kahless 7 years, 10 months ago
  3. RelatedNullFilterSpec: django-admin custom filter all/null/not null/choices by Codeko 4 years, 6 months ago
  4. Improved Pickled Object Field by taavi223 5 years, 8 months ago
  5. Generic Model by hakejam 8 years, 1 month ago

Comments

Please login first before commenting.