0

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?

3
  • Have you tried printing/logging the value of all_daily_statement_for_database prior to calling add_all()? Perhaps it is an empty list? Commented Oct 5, 2017 at 13:00
  • @mhawke No, that's not empty list, I print that and it contain values, and new object of my class contain needed values. I try add records in for, but that's don't work too. Commented Oct 5, 2017 at 13:05
  • 1
    You (probably, you did not provide the definition of Session) create 2 different sessions. The instances are added to the first, the second commits. Try session = Session() and work from there. Commented Oct 5, 2017 at 13:15

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

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.