Reputation: 661
I have function:
def set_daily_amount_once(self):
query = Session().query(AccountBalance).order_by(AccountBalance.time_valued).all()
items = [item.__dict__ for item in query]
merchants = [d['merchant_account_id'] for d in items]
unique_merchants = [i for i in Counter(merchants)]
all_merchants_and_generated_dates = []
all_statements = []
all_daily_statement_for_database = []
for i in unique_merchants:
merchant_and_generated_dates = {}
date_start = self.get_ab_trs(i,time.strftime("%Y-%m-%d"))[2]
date_end = time.strftime("%Y-%m-%d")
start = datetime.datetime.strptime(date_start, "%Y-%m-%d")
end = datetime.datetime.strptime(date_end, "%Y-%m-%d")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end - start).days+1)]
date_generated = [i.strftime("%Y-%m-%d") for i in date_generated]
merchant_and_generated_dates[i]=date_generated
all_merchants_and_generated_dates.append(merchant_and_generated_dates)
for date in date_generated:
all_statements.append(self.get_ab_trs(i,date)[0])
for statement in all_statements:
daily_statement=DailyAccountAmount(
merchant_account_id=statement['merchant_account_id'],
date=statement['date'],
balance_opened=statement['balance_opened'],
balance_closed=statement['balance_closed'],
debit_amount=statement['debit_amount'],
credit_amount=statement['credit_amount'],
total_amount=statement['total_amount'],
currency=statement['currency']
)
all_daily_statement_for_database.append(daily_statement)
try:
Session().add_all(all_daily_statement_for_database)
Session().commit()
print('Must work!')
except Exception as huston_we_have_problems:
print(huston_we_have_problems)
return all_statements
Probably, all SELECT
queries are produced, but in logs I don't see any INSERT
queries.
I haven't any exceptions, all code in try
- work, but nothing added to DB.
What can be the problem, and how to solve it?
Upvotes: 0
Views: 40
Reputation: 87064
You are not keeping a reference to your session and as a consequence it is being discarded:
Session().add_all(all_daily_statement_for_database)
Session().commit()
operates on two separate instances of Session
, not the same one, hence the commit is not effective. Try this instead:
session = Session()
session.add_all(all_daily_statement_for_database)
session.commit()
Upvotes: 1