1
我有一个File
类:SQLAlchemy的一到一对一的关系
class File(DeclarativeBase):
__tablename__ = 'files'
id = Column(Integer, primary_key=True)
name = Column(String)
mime_type = Column(String)
data = Column(LargeBinary)
,我使用我的应用程序存储附件。我也有一个Note
类:
class Note(DeclarativeBase):
__tablename__ = 'notes'
id = Column(Integer, primary_key=True)
note = Column(String)
fid = Column(Integer, ForeignKey('files.id'))
file = relationship('File', uselist=False, cascade="all, delete-orphan")
其中fid
可以NULL
如果用户没有附加的文件。除Note
之外,其他类还使用fid
列来处理附件。
不过,我想知道我怎么能记下创建更优雅。具体而言,我希望能够执行诸如Note(note=note_text,file=File(...))
之类的操作,并让SQLAlchemy负责将新条目插入files
。同样,对于现有的Note
,我希望能够做到n.file = File(...)
或n.file = None
和拥有一切只是有正在files
没有孤立项工作。
我不得不添加'单亲= TRUE'得到这个工作,但除此之外,它是你描述。 – 2012-07-29 15:39:17