This seemed like something simple to do, but I can't seem to figure out how;
I have a transactionlog table in my project which stores financial transactions. In most cases I have to write a number of these transactions in a row, all of them share a lot of properties.
So what I would like to do is instantiate one Transactionlog object, fill out the common properties and then keep adding copies of this original object to the session.
My current code is like this (simplified, it is part of a larger class-method):
t = Transactionlog()
t.tlog_newData = origin
t.tlog_ppl_id = self._contract.member.ppl_id
t.tlog_datetime = period.period_start
t.tlog_shift_datetime = period.period_end
t.tlog_artc_id = revenue_article_id
t.tlog_club_id = self._contract.member.ppl_club_id
t.tlog_ppl_mshp_id = self._contract.ppl_mshp_id
periodlabel = "{0} to {1}".format(period.period_start, period.period_end)
# linked periodical articles AB
for linkedarticle in self._contract.linkedarticles:
if linkedarticle.pmar_periodical:
if linkedarticle.pmar_free:
t.price = 0
else:
t.price = linkedarticle.article.artc_price
t.tlog_artc_id = linkedarticle.artc_id
t.tlog_comment = "{0}: {1}".format(periodlabel, linkedarticle.article.artc_description)
t.tlog_evtt_id = 'ab'
t.tlog_code = 'member_linked_article'
db_session.add(t)
# counterbook SIS
t2 = t
t2.tlog_evtt_id = 'sis'
t2.price = t.price * -1
t2.link(t)
db_session.add(t2)
t.tlog_code = None
db_session.commit()
What you see is the instantiation of the initial object t. Under the linked articles I loop through a bunch of articles and (try to) book a new transactionlog line, of type AB for each article. Every booking also has a counter booking SIS.
In the database I -do- see three records appearing, but all have the same properties, they all have tlog_evtt_id 'sis' and all have price -1. So it seems that they all get the most recently set properties.
I figured that adding to the SQLAlchemy session would generate an INSERT with the current data, and then editing the existing object and adding it again would generate a second INSERT with the new data.
So in short, what is the SQLAlchemy way to insert copies of an existing object into the database?