python - 父/子一对多区分 Null 与零子
问题描述
我的父子关系如下。我希望能够区分空子(例如尚不知道的信息)与零子。这是我目前正在采取的方法。它有效,但似乎有点麻烦。有没有更好的方法来解决这个问题?
from sqlalchemy import Column, Integer, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
engine = create_engine('sqlite:///')
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = relationship('Child', uselist=False)
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, ForeignKey('parent.id'), primary_key=True)
child_items = relationship('ChildItem')
class ChildItem(Base):
__tablename__ = 'childitems'
id = Column(Integer, ForeignKey('child.id'), primary_key=True)
Base.metadata.create_all(engine)
p = Parent()
assert(p.children is None) # Would like to be able to do something like this.
c = Child()
c.child_items.append(ChildItem())
p.children = c
assert(p.children is not None)
解决方案
推荐阅读
- python - 如何在 Zapier 中使用 Python 解析 HTML?
- reactjs - 我应该在哪里存储我的电子商务 React 应用程序的图像?
- excel - 如何使用 Excel 公式将文本“yyyy mmm ABC”转换为“yyyymm”日期格式?
- mysql - Debian 升级到 Buster 后的 MYSQL 问题
- python - 在 PyArrow 中,如何将表的行附加到内存映射文件?
- dataframe - 检查 DataFrame 名称是否包含另一个数组中的名称
- php - Laravel Http 路由请求永远发送
- java - 无法启动应用程序。Bean 创建异常
- reactjs - 问题 – “toHaveStyleRule” 带有用于测试的导入组件(jest-styled-components)
- swift - GoogleSignIn - 未找到框架 AppAuth,框架为红色 Swift