akaariai / django-reverse-unique

A ReverseUnique model field implementation for Django
BSD 3-Clause "New" or "Revised" License
22 stars 5 forks source link

Add support for ReverseUnique(null=True) #2

Closed gavinwahl closed 9 years ago

gavinwahl commented 10 years ago

When null=True, accessing a ReverseUnique that doesn't exist should raise DoesNotExist instead of returning None.

I might be missing something here if null=True controls other behavior. I did notice that it generates an inner instead of outer join in select_related. Should this behave like the reverse OneToOne field, where prefetching the reverse relationship generates an outer join but accessing it raises DoesNotExist?

akaariai commented 10 years ago

Yes, setting null=False will mean that you effectively guarantee that there is always a matching row for the join in the DB. If there isn't, then for example .select_related() can remove valid rows because it optimizes the join to INNER join.

Sometimes you might want that - your business logic ensures there is always a matching row (for example, there is always a default translation), and so using INNER joins is actually wanted. So, this should be documented (just docstring change for ReverseUnique init might be enough for now), or alternatively null=True/False should be handled as just affecting the get behavior (always pass null=True to super() in init).

akaariai commented 9 years ago

I'm not sure what this was all about. I can revisit this if there is interest.