Question

I'm vaguely familiar with the concept of migrating schemes and data with South, but now i'd like accomplish something a bit more advanced;

I have a model which holds a boolfield indicating status (active/not active), and now I need to add additional possibilities using an integerfield. The change itself is pretty straightforward using Souths schememigration.

Problem is I need to convert the statuses in the existing objects (some 400+) to integers (0 for false and 1 for true).

Is this even possible, or do I need to find another way?

Was it helpful?

Solution 3

This was pretty straight-forward after reading the docs (doh!).

schemamigration - add intfield datamigration - if bool >> int = 1, else int = 0 schemamigration - remove boolfield

OTHER TIPS

What DB system do you use ? mySQL ? PostgreSQL ? SQLite ? .. The easiest to migrate data is a SQL patch for me.

For example, with PostgreSQL:

-- DROP DEFAULT IF THERE IS ONE
ALTER TABLE app_name.model_name ALTER mycolumn TYPE integer USING CASE WHEN mycolumn=FALSE THEN 0 ELSE 1 END;
-- SET A NEW DEFAULT IF THERE WAS ONE

I had similar problem with PostgreSQL and changing migration like this helped:

def forwards(self, orm):

    # Changing field 'SomeModel.status'
    hlp = db.alter_string_set_type
    db.alter_string_set_type = 'ALTER COLUMN %(column)s TYPE %(type)s USING %(column)s::integer::smallint'
    db.alter_column('someapp_somemodel', 'status', self.gf('django.db.models.fields.PositiveSmallIntegerField')())
    db.alter_string_set_type = hlp

def backwards(self, orm):

    # Changing field 'SomeModel.status'
    hlp = db.alter_string_set_type
    db.alter_string_set_type = 'ALTER COLUMN %(column)s TYPE %(type)s USING %(column)s::integer::boolean'
    db.alter_column('someapp_somemodel', 'status', self.gf('django.db.models.fields.BooleanField')())
    db.alter_string_set_type = hlp

(of course for MySQL syntax would be different)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top