[英]Converting an existing MyISAM database to InnoDB with Django
无论如何,我可以将一个完整填充的MyISAM数据库转换为InnoDB(以某种方式创建所有外键约束,就像我从头开始运行syncdb命令一样)?
这可能有所帮助:
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])
在文件夹管理/命令下将其添加到您的应用程序/然后您可以使用manage.py命令转换所有表:
python manage.py convert_to_innodb
使用Django将MyISAM转换为InnoDB。
鉴于旧数据库在MyISAM中。
将旧数据库的数据转储到json:
$ python manage.py dumpdata contenttypes --indent=4 --natural > contenttype.json
$ python manage.py dumpdata --exclude contenttypes --indent=4 --natural > everything_else.json
删除旧数据库,然后重新创建。
在您的settings.py中添加InnoDB设置,如下所示:
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
}
}
}
创建表(django还添加关系)确保您不添加管理员用户:
$ python manage.py syncdb --migrate
现在你要清空所有旧表:
$ python manage.py sqlflush | ./manage.py dbshell
现在您可以将新数据加载到数据库中,如下所示:
$ python manage.py loaddata contenttype.json
$ python manage.py loaddata everything_else.json
你去吧 我使用Django == 1.4。
这真的与Django无关。 它完全是一个MySQL的东西,直接来自它们的文档就是这种类型的东西: http : //dev.mysql.com/doc/refman/5.5/en/converting-tables-to-innodb.html
我有一个类似的情况,我没有意识到我的托管服务提供商有这么老的MySQL版本,它仍然默认为MyISAM,但我已经设置了基本的Django表。
这是在原始问题发布后约2个月发布的: 将Legacy Django MySQL DBS从MyISAM转换为InnoDB 。
那个和leech / Trey的答案描述了在MySQL中转换表的ALTER TABLE ___ ENGINE = INNODB命令,但问题是即使转换了表,它们也没有设置的外键约束。表格首先是INNODB。
我发现,在django-admin.py和manage.py上 , python manage.py sqlall appname
“打印给定应用程序名称的CREATE TABLE和初始数据SQL语句。”
我在settings.py中查看了INSTALLED_APPS并最终运行了类似python manage.py sqlall admin auth contenttypes sessions sites messages staticfiles
东西python manage.py sqlall admin auth contenttypes sessions sites messages staticfiles
(INSTALLED_APPS中每个django.contrib.appname一个)。 这显示了初始的CREATE TABLE语句,索引和外键约束:
ALTER TABLE `django_admin_log` ADD CONSTRAINT `content_type_id_refs_id_288599e6` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`);
ALTER TABLE `django_admin_log` ADD CONSTRAINT `user_id_refs_id_c8665aa` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ALTER TABLE `auth_permission` ADD CONSTRAINT `content_type_id_refs_id_728de91f` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`);
ALTER TABLE `auth_group_permissions` ADD CONSTRAINT `permission_id_refs_id_a7792de1` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission` (`id`);
ALTER TABLE `auth_group_permissions` ADD CONSTRAINT `group_id_refs_id_3cea63fe` FOREIGN KEY (`group_id`) REFERENCES `auth_group` (`id`);
ALTER TABLE `auth_user_user_permissions` ADD CONSTRAINT `permission_id_refs_id_67e79cb` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission` (`id`);
ALTER TABLE `auth_user_groups` ADD CONSTRAINT `group_id_refs_id_f0ee9890` FOREIGN KEY (`group_id`) REFERENCES `auth_group` (`id`);
ALTER TABLE `auth_user_user_permissions` ADD CONSTRAINT `user_id_refs_id_f2045483` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ALTER TABLE `auth_user_groups` ADD CONSTRAINT `user_id_refs_id_831107f1` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ALTER TABLE `auth_message` ADD CONSTRAINT `user_id_refs_id_9af0b65a` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
在将所有表转换为ENGINE = INNODB之后,我运行了上面的外键约束,并且相信我应该让数据库处于与我的数据库默认创建INNODB表时一样的状态。
顺便提一下,正如Michael van de Waeter在他的回答中提到的,如果你想要Django默认创建的任何新表是INNODB,你应该在DATABASES dict中添加'OPTIONS': {"init_command": "SET storage_engine=INNODB",}
在settings.py中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.