尝试使用django-notifications发送通知时出现IntegrityError

时间:2012-12-28 16:55:09

标签: python django django-notification

我已经通过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`))')

发生了什么事? recipientsender都属于auth_user表。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。出于某种原因,south使用InnoDB存储引擎作为默认值,而我的所有表都默认使用MyISAM。这就是我修复它的方法:

  1. 删除表notifications_notification
  2. 删除notifications表格中south_migrationhistory的所有条目,以便稍后再次迁移notifications
  3. STORAGE_ENGINE添加到我的数据库设置:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'database',       
            'USER': 'user',           
            'PASSWORD': 'pass',       
            'HOST': '',               
            'PORT': '',               
            'STORAGE_ENGINE': 'MyISAM',
        }
    }
    
  4. 最后,再次迁移notifications

    $ python manage.py migrate notifications