Django postgresql notes

  1. Do not "pass" on
    DatabseError
    exceptions in signal processors. Bad example:
    @receiver(post_save, sender=User, dispatch_uid='user_post_save')
    def user_post_save(sender, instance, created, raw, **kwargs):
        if created and not raw:
            try:
                try:
                    flatpage = FlatPage.objects.get(url='/my_page/')
                except FlatPage.DoesNotExist:
                    flatpage = FlatPage.objects.create(**DEFAULT_PAGE)
                user = instance
    
                notify.send(user, recipient=user, verb="flatpage", target=flatpage)
            except DatabaseError:
                pass
    

    I tried to use pass in this example because syncdb failes for sqlite and "passing" fixed that problem.
    But postgresql will still fail with "Transaction aborted" if you just pass this exception. You should rollback transaction to make it work:

    from django.db import transaction
    
    @receiver(post_save, sender=User, dispatch_uid='user_post_save')
    def user_post_save(sender, instance, created, raw, **kwargs):
        if created and not raw:
            with transaction.commit_on_success():
                try:
                    try:
                        flatpage = FlatPage.objects.get(url='/my_page/')
                    except FlatPage.DoesNotExist:
                        flatpage = FlatPage.objects.create(**DEFAULT_PAGE)
                    user = instance
    
                    notify.send(user, recipient=user, verb="flatpage", target=flatpage)
                except DatabaseError:
                    transaction.rollback()
    

  2. If you use South then you could also encounter problems when one of your apps depends on migrations in other app. For example your
    clients.ContactPerson
    has FK to
    projects.Project
    and your
    projects.Project
    has FK to
    client.Client
    . In real situation this relations will be splited to several migrations and there should be at least one migration where
    clients
    app is not dependent on
    projects
    app or vice versa. In this case to make migrations work under postgresql, you should specify dependencies:

    class Migration:
        
        depends_on = (
            ("clients", "0001_initial"),
        )