Blog

Blog

PHODAL

Django Sqlite3 MySQL迁移问题一览

往MySQL迁移的过程中遇到越来越多的问题。。

MySQL Django 数据库引擎

Django “Cannot add or update a child row: a foreign key constraint fails”

mysql> SHOW CREATE TABLE blog_blogcategory;
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table             | Create Table                                                                                                                                                                                                                                                                                                                                                                              |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| blog_blogcategory | CREATE TABLE `blog_blogcategory` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `site_id` int(11) NOT NULL,
  `title` varchar(500) NOT NULL,
  `slug` varchar(2000) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `blog_blogcategory_99732b5c` (`site_id`),
  CONSTRAINT `site_id_refs_id_93afc60f` FOREIGN KEY (`site_id`) REFERENCES `django_site` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

也就是这个问题a child row a foreign key constraint fails

上面用的索引是InnoDB而有些是MyISAM

方法是在settings.py里添加

    'OPTIONS': {
       "init_command": "SET storage_engine=MyISAM",
    },

于是slave就变成这样子了

    "slave": {
        # Ends with "postgresql_psycopg2", "mysql", "sqlite3" or "oracle".
        "ENGINE": "django.db.backends.mysql",
        # DB name or path to database file if using sqlite3.
        "NAME": "phodal",
        # Not used with sqlite3.
        "USER": "root",
        # Not used with sqlite3.
        "PASSWORD": "",
        # Set to empty string for localhost. Not used with sqlite3.
        "HOST": "",
        # Set to empty string for default. Not used with sqlite3.
        "PORT": "",
        'OPTIONS': {
           "init_command": "SET storage_engine=MyISAM",
        },
    },

MySQL Django 编码

 Warning: Incorrect string value: '\xF0\x9F\x92\xB0' ...' for column 'content' at row 1

MySQL utf8mb4

utf8mb4兼容utf8,且比utf8能表示更多的字符。

于是只能重来了。。。。。

   CREATE DATABASE phodal CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

到此总算解决了大部分的文章问题了,然而还没有完。。。

编码问题

   UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-10: ordinal not in range(128)

最后找到原因是...

 全部文章(测试)

不存在

DoesNotExist: ContentType matching query does not exist.

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签