6

I have a class:

class Chart(Base):
   __tablename__ = 'chart'
   id = C('chart_id', Integer, primary_key=True)
   element_id = C(Integer, ForeignKey('element.element_id'))
   element = relationship(Element)
   name = C(String)

   def __init__(self, name):
      self.name = name

Usage is pretty common,

chart = Chart('Some name')
chart.element_id = element_id

But chart.element is None after setting element_id. Is there any way to auto-load this relation for new object before flush/commit?

1 Answer 1

6

The best option is

chart = Chart('Some name')
chart.element = element

Assign direct object to the relation ship. If you are assign element_id then until it flush it will be in memory. Internally it will fire a query SELECT * FROM ELEMENT WHERE ELEMENT.id = element_id but that element_id data is not store or it will be in memory.

So i suggest direct assign object if you don't want to flush.

Hope this will help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reponse. I'm using this way now. My function receives element_id, and I get element by myself to set it in a Chart object (flush is done internally). It looks it's the only one way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.