SQLAlchemy query to return only n results?
I have been googling and reading through the SQLAlchemy documentation but haven’t found what I am looking for.
Must one close session if isolation_level=AUTOCOMMIT?
In configuration below with isolation_level
equal to AUTOCOMMIT
what will happen to session if we don’t close it? Is it safe?
How to select only related model?
I need to select only Phone.user
when selecting Phone model
:
Using a Table object together with an ORM-mapped class
At the end of this section it is mentioned that:
How do you dynamically add multiple options to a sqlalchemy 2.0 select query
I’m trying to build a dynamic query in Async Sqlalchemy 2.0. The options come in as a list like:
creating a task based on data on weekly marks in the next period, how to do this?
activity_mtm_week = Table( “activity_mtm_week”, Base.metadata, Column(“activity_id”, ForeignKey(“activity.id”), primary_key=True), Column(“week_id”, ForeignKey(“week.id”), primary_key=True), ) class Activity(Base): __tablename__ = “activity” id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(15)) weeks: Mapped[List[“Week”]] = relationship( secondary=activity_mtm_week, back_populates=”activities” ) activity_days: Mapped[List[“Activity_day”]] = relationship( cascade=”all, delete, delete-orphan” ) class Week(Base): __tablename__ = “week” id: Mapped[int] = mapped_column(primary_key=True) week_day: Mapped[str] = mapped_column(String(2)) activities: Mapped[List[“Activity”]] […]
Storing SqlAlchemy models for database with >20 tables
I’m working on a pet project for a web shop that uses SQLAlchemy (SA) and connects to a database with over 25 tables (thus, I have 25+ SA models – one for each table).
SQLAlchemy async engine and relationships
I am building a RBAC web app with FastAPI. This led me to model implementation below. Based on it, I wrote some tests to validate the implementation. Among others, I required to get permissions of an user based on current instance. Since model instance would be already available as soon I call the method, I thought, it would be straightforward to call required method to obtain the permissions, like a regular class instance. It did not work, but raised error sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place?
. ChatGPT suggested me to perform operation as on implementation, which worked, but made me a bit sad on limitations of the library for async operations. I step further would be to use a more adequate library SQLModel (or even this async-SQLModel) as a substitute, but I feel not so comfortable about that. Had you the same experience with async engine so far?
flask won’t let me import sql alchemy
every time I try to run my flask app I keep getting “cannot import name ‘SQLAlchemy’ from ‘flask’ “(/Users/glitxh/LETSFIXPHONES/.repair/lib/python3.12/site-packages/flask/init.py)
Is it possible to get the table and field name, and current record id from within sqlalchemy field type definition?
I need to create custom sqlalchemy type which would receive path to the file and while saving the object it would move the file to the special place which contains in its path the name of the field, the name of the table and the id of the curent record.