feel free to hang here, note that some of the "chat" happens on libera.chat #sqlalchemy
I am struggling with some async session/connection issues with asyncpg with 1.4.26. I have the following:
async_session: sessionmaker = sessionmaker(
bind=engine,
class_=AsyncSession,
autocommit=False,
autoflush=False,
expire_on_commit=False,
)
...
async with async_session() as db:
...
an_obj.update_list_words(db, myman)
...
async def update_list_words(
self, db: AsyncSession, manager: EnrichmentManager
):
...
for freq, word in word_list:
data.append(UserListDict(word=word, import_frequency=int(freq)))
temp_table = f"import_{self.id}".replace("-", "_") # self.id is a GUID
sql = f"""CREATE TEMP TABLE {temp_table} (word text, import_frequency int, word_id int null, freq float null)"""
await db.execute(text(sql))
await db.execute(text( f"""INSERT INTO {temp_table} (word, import_frequency) values (:word, :import_frequency) """), data,)
await db.execute(text(
f"""UPDATE {temp_table}
SET word_id = mitable.id
FROM mitable
WHERE word = mitable.source_text
and mitable.from_lang = :from_lang
and mitable.to_lang = :to_lang"""
),
{"from_lang": manager.from_lang, "to_lang": manager.to_lang},
)
...
await db.execute(text(f"DROP TABLE {temp_table}"))
await db.commit()
I am getting some strange behaviour that I just can't understand. For example, I just got a 'asyncpg.exceptions.UndefinedTableError'>: relation "import_19fd0dbb_0321_4124_a810_22a4efee7ab1" does not exist
when it gets to the UPDATE which I am struggling to understand how it's possible. Any pointers? It works sometimes, typically the first time it gets called. Then it fails with a few different errors.
with Session.begin() as session:
while has_remaining_operations():
operations = session.query(Operation) \
.filter(
Operation.id_user == user.id and
Operation.processed_at is None
) \
.order_by(Operation.created_at.asc()) \
.limit(100) \
.all()
for op in operations:
# code to execute
op.processed_at = datetime.now().isoformat()
processed_at
modification doesn't get applied when session.query
is executed
Operation.created_at
, grab the last one from each chunk and add a WHERE Operation.created_at > last_created_at_from_previous_chunk
.filter
clause. It should have looked like this: .filter(and_(
SavedOp.id_user == user.id,
SavedOp.processed_at.is_(None)
)) \
is_(None)
is the correct form
await session.execute(
sqlalchemy.update(
some_table
).where(
some_table.id.in_(ids)
).values(
[{'a':.., ...}, {'a':.., ...},...]
),
)
UPDATE construct does not support multiple parameter sets.
cursor.execute(statement, parameters)
where cursor
is a psycopg2 cursor
parameters
is an empty dict
cur.execute('select 1', {})
where type(cur)
is psycopg2.extensions.cursor
Hi,
We are trying to use SQLAlchemy compile
to generate queries in different dialects. It is somehow not working for complex queries.
QUERY
SELECT partsupp.ps_supplycost, region.r_regionkey FROM region JOIN partsupp ON
(partsupp.ps_partkey 698) < region.r_regionkey
SELECT OBJECT
<class 'sqlalchemy.sql.selectable.Select'>
SELECT partsupp.ps_supplycost, region.r_regionkey
FROM region JOIN partsupp ON
(partsupp.ps_partkey + %(ps_partkey_1)s) < region.r_regionkey
USAGE
relationship(..., backref="...")
populate parent class before using it?