如何在python2.7的ORM中映射具有外键属性的列的空值

2024-03-29 10:38:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python2.7中使用sqlalchemy作为ORM编写了以下代码:

   if j.request.component:
        comp_prod = (j.request.component.name, j.request.component.product.name)
   else:
        comp_prod = ('None', 'None')

但崩溃时出现以下错误:

File "/remote/vtghome9/vgtools2/python2.5/lib/python2.5/site-packages/ChImE-1.1-py2.5.egg/kachori/extra/streams.py", line 4110
    comp_prod = (j.request.component.name, j.request.component.product.name)
AttributeError: 'NoneType' object has no attribute 'name'

我检查了“request”表,对于表中的一行,“component”为空。 我的问题是组件条目是否为NULL,也就是“i”。请求.组件“不应该是对的吗?它不应该在那个if条件下转到其他部分吗?你知道吗


Tags: 代码namenoneifsqlalchemyrequest错误orm