How to define two relationships to the same table in SQLAlchemy

As of version 0.8, SQLAlchemy can resolve the ambiguous join using only the foreign_keys keyword parameter to relationship. publish_user = relationship(User, foreign_keys=[publishing_user_id], backref=backref(‘pages’, order_by=id)) edit_user = relationship(User, foreign_keys=[last_edit_user_id]) Documentation at http://docs.sqlalchemy.org/en/rel_0_9/orm/join_conditions.html#handling-multiple-join-paths

How to delete records from many-to-many (secondary) table in SQLAlchemy?

Try this: post = db.session.query(Post).get(1) post.tags = [] db.session.commit() Here we are redefining the collection post.tags to the empty array and committing the changes. To explain this I’ll refer to the SQLAlchemy docs: Collections in SQLAlchemy are transparently instrumented. Instrumentation means that normal operations on the collection are tracked and result in changes being written … Read more

Alembic – sqlalchemy does not detect existing tables

I had this exact same issue – I don’t know if it still affects you. For me, the problem was caused because the schema I was using was not the default – I think the same thing is happening for you, since you’re using a “Products” schema. I posted an issue at: https://bitbucket.org/zzzeek/alembic/issue/281/autogenerate-fails-to-detect-existing And with … Read more

SQLALchemy dynamic filter_by

Instead of using filter_by I would recommend using filter, it gives you a lot more options. For example (from the manual): db.session.query(MyClass).filter( MyClass.name == ‘some name’, MyClass.id > 5, ) In relation to your case: filters = ( Transaction.amount > 10, Transaction.amount < 100, ) db.session.query(Transaction).filter(*filters)

Connecting to an Oracle database using SQLAlchemy

You don’t need to import cx_Oracle anymore. The newer version of the sqlalchemy module calls the function cx_Oracle.makedsn(). Have a look: from sqlalchemy.engine import create_engine DIALECT = ‘oracle’ SQL_DRIVER = ‘cx_oracle’ USERNAME = ‘your_username’ #enter your username PASSWORD = ‘your_password’ #enter your password HOST = ‘subdomain.domain.tld’ #enter the oracle db host url PORT = 1521 … Read more

How to get month and year from Date field in sqlalchemy?

You can use following constructs to filter the Date column using either year or month: .filter(extract(‘year’, Foo.Date) == 2012) .filter(extract(‘month’, Foo.Date) == 12) And group_by is also possible: .group_by(sqlalchemy.func.year(Foo.Date), sqlalchemy.func.month(Foo.Date)) Now I haven’t tested it, but I assume that this might be slow because these queries result in full table scans, therefore I suggest you … Read more

Creating “zero state” migration for existing db with sqlalchemy/alembic and “faking” zero migration for that existing db

alembic revision –autogenerate inspects the state of the connected database and the state of the target metadata and then creates a migration that brings the database in line with metadata. If you are introducing alembic/sqlalchemy to an existing database, and you want a migration file that given an empty, fresh database would reproduce the current … Read more