SQLAlchemy提交primaryjoin列

时间:2017-04-09 20:34:33

标签: python-2.7 orm sqlalchemy

我有两张桌子“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}

0 个答案:

没有答案