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