Search code examples
pythondjangodjango-southdatabase-migration

duplicate table (identical) including primary key - using django south


I'm using south data migration to move data from one table to another (between django apps)

Its moving all the data as it suppose to but, problem is its not duplication the primary key (ID), instead creating a new one (in the new table) which make sense..

But how can I make a new table identical, including the id field (which is default, not defined in models)

using mysql


Solution

  • Create the table using LIKE in forwards part, insert everything from old table to the new one. In backwards part just drop the new table.

    def forwards(self, orm):
        db.execute("CREATE TABLE new_table LIKE old_table")
        db.execute("INSERT new_table SELECT * old_table")
    
    def backwards(self, orm):
        db.drop_table('new_table')
    

    Hope that helps.