I’m using SQLAlchemy for a project, and need to be able to specify a session variable/setting for one specific call for performance reasons: I can of course do this in MySQL directly (on the shell), but how do I set this session variable in a SQLAlchemy session? Answer Use a session event to execute an arbitrary SQL statement on each
Tag: sqlalchemy
How to execute raw SQL in Flask-SQLAlchemy app
How do you execute raw SQL in SQLAlchemy? I have a python web app that runs on flask and interfaces to the database through SQLAlchemy. I need a way to run the raw SQL. The query involves multiple table joins along with Inline views. I’ve tried: But I keep getting gateway errors. Answer Have you tried: or: Note that db.engine.execute()
sqlalchemy generic foreign key (like in django ORM)
Does sqlalchemy have something like django’s GenericForeignKey? And is it right to use generic foreign fields. My problem is: I have several models (for example, Post, Project, Vacancy, nothing special there) and I want to add comments to each of them. And I want to use only one Comment model. Does it worth to? Or should I use PostComment, ProjectComment
AttributeError while querying: Neither ‘InstrumentedAttribute’ object nor ‘Comparator’ has an attribute
The following code: is giving me this error: Any explanations, as to why this is happening, and guidance to how such a thing could be achieved? Answer This is because you are trying to access bar from the FooBar class rather than a FooBar instance. The FooBar class does not have any bar objects associated with it–bar is just an
sqlalchemy OperationalError: (OperationalError) (1045, “Access denied for user
I am trying to create a remote database using mysql on an Ubuntu machine running 12.04. It has a root user with remote login enabled.I have started the server. output of shows From the client machine that also runs Ubuntu 12.04 I use a python script to connect to the remote mysql database using sqlalchemy. The create_engine() call is failing
sqlalchemy.exc.ArgumentError: Can’t load plugin: sqlalchemy.dialects:driver
I am trying to run alembic migration and when I run It fails saying the database url is and I even have psycopg2 installed in my virtualenv Whay could be causing this issue? Answer Here’s how to produce an error like that: so I’d say you aren’t actually using the postgresql URL you think you are – you probably are
Altering an Enum field using Alembic
How can I add an element to an Enum field in an alembic migration when using a version of PostgreSQL older than 9.1 (which adds the ALTER TYPE for enums)? This SO question explains the direct process, but I’m not quite sure how best to translate that using alembic. This is what I have: The above unfortunately only produces ALTER
SQLAlchemy ORM select multiple entities from subquery
I need to query multiple entities, something like session.query(Entity1, Entity2), only from a subquery rather than directly from the tables. The docs have something about selecting one entity from a subquery but I can’t find how to select more than one, either in the docs or by experimentation. My use case is that I need to filter the tables underlying
How to get month and year from Date field in sqlalchemy?
The result should be Date object Since the day cannot be ‘removed’, set it to say, 1st day of the month. Leaving only Month, Year Answer You can use following constructs to filter the Date column using either year or month: And group_by is also possible: Now I haven’t tested it, but I assume that this might be slow because
Setting a default value in sqlalchemy
I would like to set a column default value that is based on another table in my SQLAlchemy model. Currently I have this: What I need is (roughly) this: How can I implement this in SQLAlchemy? Answer The documentation gives the following possibilities for default: A scalar, Python callable, or ClauseElement representing the default value for this column, which will