【问题标题】:IntegrityError while trying to send a notification with django-notifications尝试使用 django-notifications 发送通知时出现 IntegrityError
【发布时间】:2012-12-28 16:55:09
【问题描述】:

我已经通过 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 表。任何帮助将不胜感激。

【问题讨论】:

  • 第一个参数应该是收件人,使用actor=sender或者重新排序参数notify.send(recipient, sender, verb='A test')
  • @AamirAdnan 这也不起作用。如果尝试notify.send(recipient, actor=sender, verb='A test'),那么我会得到KeyError "recipient"。如果我使用notify.send(recipient, sender, verb='A test') 我会得到TypeError: send() takes exactly 2 arguments (4 given)
  • 试试这个notify.send(recipient=recipient, sender=sender, verb='A test')这应该可以。
  • @AamirAdnan 我再次得到 IntegrityError。谢谢,看来问题出在其他地方
  • @AamirAdnan 不,它期待收件人关键字,所以我收到 KeyError "recipient"

标签: python django django-notification


【解决方案1】:

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

  1. 删除表notifications_notification
  2. 删除south_migrationhistory 表中notifications 的所有条目,以便稍后再次迁移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
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2014-03-22
    相关资源
    最近更新 更多