我已经通过pip安装了django-notifications
,将应用添加到我的INSTALLED_APPS
:
INSTALLED_APPS = (
'django.contrib.auth',
...
'notifications',
...
)
更新了我的URLConf:
import notifications
urlpatterns = patterns('',
...
('^inbox/notifications/', include(notifications.urls), namespace='notifications'),
...
)
由于我安装了south
,因此我会迁移架构:
$ python manage.py migrate notifications
...
...
$ python manage.py migrate --list
notifications
(*) 0001_initial
(*) 0002_auto__add_field_notification_data
(*) 0003_auto__add_field_notification_unread
(*) 0004_convert_readed_to_unread
(*) 0005_auto__del_field_notification_readed
(*) 0006_auto__add_field_notification_level
...
现在,我正在尝试通过Django shell手动创建通知,但我得到了IntegrityError
:
[1]: from django.contrib.auth.models import User
[2]: from notifications import notify
[3]: recipient = User.objects.get(username="admin")
[4]: sender = User.objects.get(username="guest")
[5]: notify.send(sender, verb='A test', recipient=recipient)
...
...
...
IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`myapp`.`notifications_notification`, CONSTRAINT `recipient_id_refs_id_5c79cb54` FOREIGN KEY (`recipient_id`) REFERENCES `auth_user` (`id`))')
发生了什么事? recipient
和sender
都属于auth_user
表。任何帮助将不胜感激。
答案 0 :(得分:0)
好的,我发现了问题。出于某种原因,south
使用InnoDB存储引擎作为默认值,而我的所有表都默认使用MyISAM。这就是我修复它的方法:
notifications_notification
notifications
表格中south_migrationhistory
的所有条目,以便稍后再次迁移notifications
将STORAGE_ENGINE
添加到我的数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': '',
'PORT': '',
'STORAGE_ENGINE': 'MyISAM',
}
}
最后,再次迁移notifications
:
$ python manage.py migrate notifications