thenorm thenorm - 3 months ago 14
Python Question

SqlAlchemy can't find record

I have a table called

DR
with columns
dag_id
,
execution_date
, and
run_id
. I'd like to find a specific record that I know to exist in the table.

In my unit tests, I create one entry in the table and then try to find it again.

tmp = session.query(DR).filter(DR.dag_id == self.dag_id).first()

print(self.dag_id == tmp.dag_id)
print(self.execution_date == tmp.execution_date)
print(self.run_id == tmp.run_id)


All three print statements print true, but then when perform the following query, no records are found:

session.query(DR).filter(
DR.dag_id == self.dag_id,
DR.execution_date == self.execution_date,
DR.run_id == self.run_id
).first()


Does anyone know why this is happening?

Thanks!

Answer

Have you tried the following query?

session.query(DR).filter(
    DR.dag_id == self.dag_id,
    DR.run_id == self.run_id
).first()

If that works the problem may be python datetime equality is being evaluated differently to your database datetime equality.

Comments