我有两张桌子“CashDoc”和“Entry”。
class Entry(Base):
__tablename__ = 'journal_entries'
id = Column(Integer, primary_key=True)
credit = Column('credit', String)
debit = Column('debit', String)
amount = Column('amount', Float)
property = sql.Column('property', sql.types.JSON, default={}, server_default='{}')
created = Column(DateTime, default=datetime.now(), server_default=text('now()'))
edited = Column(DateTime, server_default=text('now()'), onupdate=text('now()'))
CashDoc包含带有primaryjoin
的列“条目”class CashDoc(Base):
__tablename__ = 'cash_docs'
id = Column(Integer, primary_key=True)
is_deleted = Column(Boolean, default=False)
amount = Column(Float)
entries = relationship(Entry, primaryjoin="foreign(Entry.property.op('->>')('cashdoc_id')).cast(Integer) == CashDoc.id.cast(Integer)")
created = Column(DateTime, server_default=text('now()'))
edited = Column(DateTime, server_default=text('now()'), onupdate=text('now()'))
然后我可以像这样创建CashDoc和条目
new_doc = CashDoc(params)
new_doc.entries.append(Entry('62', '50', new_doc.amount))
但在session.commit()property
字段之后只有id。不是json。
如何以property
格式保存{'cashdoc_id': id}
?