feel free to hang here, note that some of the "chat" happens on libera.chat #sqlalchemy
oh, to anyone interested in sqlalchemy_utils.register_composites()
+ async
cannot drop sequence sequence_ticket_number because other objects depend on it
ticket_number = db.Column(
db.Integer, sequence_ticket_number, server_default=sequence_ticket_number.next_value(), autoincrement=True, nullable=False)
sequence_ticket_number.next_value()
part resolves
Hi all.
Using ORM.
I have a O2M relationship that I set to lazy="raise"
(addresses
in User
table).
Now I want to use the relationship User.addresses
in a query:result = await async_session.execute(User.addresses.statement)
But it complains saying AttributeError: 'InstrumentedList' object has no attribute 'statement'
I don't want to change the lazy
so that I don't accidentally call it.
lzay="dynamic"
Another question: How to joinedload
and selectinload
on the same table?
Example:
User - O2M - Addresses
User - M2O (FK) - Group
This doesn't work:select(User).options(joinedload(User.group).selectinload(User.addresses))
Group
select(User).options(joinedload(User.group).selectinload(User.addresses))
you should not chain them since aggresses is not part of Group. they should be separated by comma, since are both "top level" (of user) select(User).options(joinedload(User.group), selectinload(User.addresses))
lazy="raise"
, which would raise an exception each time I try to access the respective attribute. Is there a way to load such an "unloaded" relationship after I have already obtained the object from the ORM? According to the documentation, Session.refresh()
doesn't automatically refresh unloaded relationships. How do I load the relationship, then? I'm aware of joinedload()
& friends, but in case of a large object graph I would like to avoid eagerly loading the whole database with every single query.
async with aiofiles.open(
directory, mode="r", encoding="utf-8", newline=""
) as afp:
async for new in AsyncDictReader(afp):
query = ScheduleService(
**{
"id": new["id"],
"name": new["name"],
"type": new["type"],
"title": new["title"],
"description": new["description"],
"date": datetime.strptime(
new["date"], settings.DATE
).date(),
"there_is": datetime.strptime(
new["there_is"], settings.DATETIME_FORMAT
),
"created_at": datetime.now(),
"sch_s_owner": new["sch_s_owner"],
"sch_s_service_id": new["sch_s_service_id"],
}
)
# ..
session.add_all(query)
session.refresh([query])
await session.commit()
self.mergte_result(..., load=False)
I get: AttributeError: '<MyModel>' object has no attribute '_sa_instance_state'
does anyone know if this is me making a mistake/wrong-assumption or is the tutorial that is missing something?