Code

Opened 5 years ago

Closed 5 years ago

#10628 closed (invalid)

RelatedManager and NULL

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1-beta
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

My Model:

class Active(models.Model):
 post    = models.ForeignKey(Post, related_name='activeposts')
 rubrica = models.ForeignKey(Rubrica, related_name='activeposts', null=True, blank=True)
 type    = models.ForeignKey(Type, related_name='activeposts', null=True, blank=True)

Code:

>>> p = Post.objects.get(pk=1206535731)
>>> p.activeposts.filter(type__pk=2, rubrica__isnull=True)

return empty result. but a have data in database.

>>> p.activeposts.filter(type__exact=2, rubrica__isnull=True).query.as_sql()
('SELECT `doska_active`.`id`, `doska_active`.`post_id`, `doska_active`.`rubrica_id`, `doska_active`.`type_id` FROM `doska_active` LEFT OUTER JOIN `doska_rubrica` ON (`doska_active`.`rubrica_id` = `doska_rubrica`.`id`) WHERE (`doska_active`.`post_id` = %s  AND `doska_active`.`type_id` = %s  AND `doska_rubrica`.`id` IS NULL)', (1206535731, 2))

Problem in AND doska_rubrica.id IS NULL

Attachments (0)

Change History (4)

comment:1 Changed 5 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Your filter is asking for things where rubrica__isnull=True, which is why that comparison to NULL is in the query. That's the correct SQL for that filter.

comment:2 Changed 5 years ago by anonymous

I need:
AND doska_active.type_id = %s AND doska_active.rubrica_id IS NULL

comment:3 Changed 5 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:4 Changed 5 years ago by mtredinnick

  • Resolution set to invalid
  • Status changed from reopened to closed

Please ask support questions on the django-users mailing list. The SQL output you are getting for the filter you have given is not incorrect. There is no bug here.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.