How to specify a relation in SQLAlchemy where one condition requires the column to be null?

Not sure what the correct title should be for this question. I have the following diagram:

  • Questions have a one-to-many relationship with WorkItems.
  • WorkItems have one, one (or one) relation to LineItems.

I am trying to create the following relationship between questions and work items

Matter.unbilled_work_items = orm.relation(WorkItem,
  primaryjoin = (Matter.id == WorkItem.matter_id) and (WorkItem.line_item_id == None),
  foreign_keys = [WorkItem.matter_id, WorkItem.line_item_id],
  viewonly=True
)

It throws:

AttributeError: '_Null' object has no attribute 'table'

It seems that the second sentence in primaryjoin returns an object of type _Null, but it seems to be expecting something with the "table" attribute.

It seems like it should be pretty simple for me, will I miss something obvious?

Update

The answer was to change the line primaryjointo:

primaryjoin = "and_(Matter.id == WorkItem.matter_id, WorkItem.line_item_id == None)"
+3
2

and_, and :

and_((Matter.id == WorkItem.matter_id), (WorkItem.line_item_id == None))
+5

_Null , , . , unbilled_work_items , .

# like this

@property
def unbilled_work_items(self):
  return Session.object_session(self).query.filter(...).all()
+1

Source: https://habr.com/ru/post/1746137/


All Articles