往MySQL迁移的过程中遇到越来越多的问题。。
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",
},
},
Warning: Incorrect string value: '\xF0\x9F\x92\xB0' ...' for column 'content' at row 1
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 Idea墙, 也许,你会遇到心仪的项目