Converting an existing MyISAM database to InnoDB with Django

后端 未结 4 1650
鱼传尺愫
鱼传尺愫 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:18

    Converting MyISAM to InnoDB with Django.

    Given the old database is in MyISAM.

    Dump the data of old database to json with:

    $ python manage.py dumpdata contenttypes --indent=4 --natural > contenttype.json
    $ python manage.py dumpdata --exclude contenttypes --indent=4 --natural > everything_else.json
    

    Delete the old database, and create it again.

    Add InnoDB settings in your settings.py like this:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'STORAGE_ENGINE': 'InnoDB',
            'NAME': 'yourdbname',
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
            'OPTIONS': {
                'init_command': 'SET storage_engine=InnoDB',  # better to set this in your database config, otherwise django has to do a query everytime
            }
        }
    }
    

    Create tables (django also adds the relations) Make sure you don't add an admin user:

    $ python manage.py syncdb --migrate
    

    Now you want to empty all the old tables:

    $ python manage.py sqlflush | ./manage.py dbshell
    

    Now you can load the new data into the database like so:

    $ python manage.py loaddata contenttype.json
    $ python manage.py loaddata everything_else.json
    

    There you go. I used Django==1.4 for this.

提交回复
热议问题