Converting an existing MyISAM database to InnoDB with Django

后端 未结 4 1605
鱼传尺愫
鱼传尺愫 2021-02-09 01:09

Is there anyway I can convert a full populated MyISAM database to InnoDB (in a way that will create all foreign key contraints, same way it would be if I ran the syncdb command

4条回答
  •  悲哀的现实
    2021-02-09 01:32

    This might help:

    from django.core.management.base import BaseCommand
    from django.db import connections
    
    
    class Command(BaseCommand):
    
        def handle(self, database="default", *args, **options):
    
            cursor = connections[database].cursor()
    
            cursor.execute("SHOW TABLE STATUS")
    
            for row in cursor.fetchall():
                if row[1] != "InnoDB":
                    print "Converting %s" % row[0],
                    print cursor.execute("ALTER TABLE %s ENGINE=INNODB" % row[0])
    

    Add that to your app under the folders management/commands/ Then you can convert all your tables with a manage.py command:

    python manage.py convert_to_innodb
    

提交回复
热议问题