0

I've installed django-notifications via pip, added the app to my INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.auth',
    ...
    'notifications',
    ...
)

Updated my URLConf:

import notifications

urlpatterns = patterns('',
    ...
    ('^inbox/notifications/', include(notifications.urls), namespace='notifications'),
    ...
)

Since I have south installed, I migrate the schema:

$ 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

 ...

Now, I'm trying to manually create a notification via Django shell but I'm getting an 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`))')

What's going on? Both, recipient and sender belong to auth_user table. Any help will be much appreciated.

César
  • 9,939
  • 6
  • 53
  • 74
  • the first argument should be the recipient, use `actor=sender` or re order arguments `notify.send(recipient, sender, verb='A test')` – Aamir Rind Dec 28 '12 at 17:16
  • @AamirAdnan that's not working either. I f a try `notify.send(recipient, actor=sender, verb='A test')` then I get a `KeyError "recipient"`. If I use `notify.send(recipient, sender, verb='A test')` I get `TypeError: send() takes exactly 2 arguments (4 given)` – César Dec 28 '12 at 17:22
  • try this `notify.send(recipient=recipient, sender=sender, verb='A test')` this should work. – Aamir Rind Dec 28 '12 at 17:26
  • @AamirAdnan with that I get the IntegrityError again. Thanks, it seems the problem is somewhere else – César Dec 28 '12 at 17:27
  • @AamirAdnan nope, it is expecting recipient keyword so I'm getting KeyError "recipient" – César Dec 28 '12 at 17:45
  • @AamirAdnan for some reason, it is working in my home laptop but not in my work PC – César Dec 28 '12 at 22:58
  • Just un-install the django-notifications package and re-install it on your work pc. That's the advice I can give at this moment. – Aamir Rind Dec 28 '12 at 23:22
  • Integrity errors are usually database-related in my experience, which might explain why the code itself works on a different setup – ppetrid Dec 29 '12 at 04:20

1 Answers1

0

Ok, I found the problem. For some reason south was using InnoDB storage engine as a default while all my tables have MyISAM as a default. This is how I fixed it:

  1. Drop table notifications_notification
  2. Delete all entries of notifications on south_migrationhistory table so you can migrate notifications again later
  3. Add STORAGE_ENGINE to my database settings:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'database',       
            'USER': 'user',           
            'PASSWORD': 'pass',       
            'HOST': '',               
            'PORT': '',               
            'STORAGE_ENGINE': 'MyISAM',
        }
    }
    
  4. Finally, migrate notifications again:

    $ python manage.py migrate notifications
    
César
  • 9,939
  • 6
  • 53
  • 74