Questions tagged [sqlalchemy]

SQLAlchemy is a Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

SQLAlchemy provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

NOTE: PLEASE report bugs / advanced issues as a GitHub discussion or on the sqlalchemy mailing list - much more comprehensive help for complex issues is available there. Please follow these guidelines when posting.

Example

metadata = MetaData()
engine = sqlalchemy.create_engine('mysql://user:pass@localhost/sql_db') # session handler

names = Table('names', metadata,
  Column('id', Integer),
  Column('name', String(60))
)

metadata.create_all(engine) # create tables in case they do not exist

# ask user for id and name
_id = int(input('indicate id: '))  # use `_id`, as `id` is a reserved word in python
name = input('indicate name: ')

ins = names.insert().values(name=name, id=_id) # insert values in table

References

Books:

Talks:

22992 questions
131
votes
6 answers

How to query database by id using SqlAlchemy?

I need to query a SQLAlchemy database by its id something similar to User.query.filter_by(username='peter') but for id. How do I do this? [Searching over Google and SO didn't help]
user507220
128
votes
8 answers

Best way to do enum in Sqlalchemy?

I'm reading about sqlalchemy and I saw following code: employees_table = Table('employees', metadata, Column('employee_id', Integer, primary_key=True), Column('name', String(50)), Column('manager_data', String(50)), …
Timmy
  • 12,468
  • 20
  • 77
  • 107
128
votes
11 answers

method of iterating over sqlalchemy model's defined columns?

I've been trying to figure out how to iterate over the list of columns defined in a SQLAlchemy model. I want it for writing some serialization and copy methods to a couple of models. I can't just iterate over the obj.__dict__ since it contains a lot…
Rick
  • 15,484
  • 5
  • 25
  • 29
127
votes
5 answers

sqlalchemy filter multiple columns

How do I combine two columns and apply filter? For example, I want to search in both the "firstname" and "lastname" columns at the same time. Here is how I have been doing it if searching only one column: query =…
teggy
  • 5,995
  • 9
  • 38
  • 41
127
votes
3 answers

SQLAlchemy create_all() does not create tables

I'm trying to integrate PostgreSQL and SQLAlchemy but SQLAlchemy.create_all() is not creating any tables from my models. My code: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app =…
Paul
  • 6,641
  • 8
  • 41
  • 56
126
votes
10 answers

SQLAlchemy equivalent to SQL "LIKE" statement

A tags column has values like "apple banana orange" and "strawberry banana lemon". I want to find the SQLAlchemy equivalent statement to SELECT * FROM table WHERE tags LIKE "%banana%"; What should I pass to Class.query.filter() to do this?
Gary Oldfaber
  • 1,582
  • 3
  • 14
  • 19
124
votes
2 answers

Multiple columns index when using the declarative ORM extension of sqlalchemy

According to the documentation and the comments in the sqlalchemy.Column class, we should use the class sqlalchemy.schema.Index to specify an index that contains multiple columns. However, the example shows how to do it by directly using the Table…
yorjo
  • 1,699
  • 3
  • 12
  • 14
120
votes
14 answers

Is it possible to store the alembic connect string outside of alembic.ini?

I'm using Alembic with SQLAlchemy. With SQLAlchemy, I tend to follow a pattern where I don't store the connect string with the versioned code. Instead I have file secret.py that contains any confidential information. I throw this filename in my…
Doug T.
  • 64,223
  • 27
  • 138
  • 202
115
votes
6 answers

flake8 complains on boolean comparison "==" in filter clause

I have a boolean field in the mysql db table. # table model class TestCase(Base): __tablename__ = 'test_cases' ... obsoleted = Column('obsoleted', Boolean) To get the count of all the non-obsoleted test cases, that can be done simply…
Jruv
  • 1,438
  • 2
  • 9
  • 10
114
votes
1 answer

What is the difference between the declarative_base() and db.Model?

The quickstart tutorial for the Flask-SQLAlchemy plugin instructs users to create table models inheriting the db.Model class, e.g. app = Flask(__main__) db = SQLAlchemy(app) class Users(db.Model): __tablename__ = 'users' ... However, the…
drs
  • 5,679
  • 4
  • 42
  • 67
112
votes
1 answer

SQLAlchemy versioning cares about class import order

I was following the guide here: http://www.sqlalchemy.org/docs/orm/examples.html?highlight=versioning#versioned-objects and have come across an issue. I have defined my relationships like: generic_ticker = relation('MyClass',…
Nick Holden
  • 3,639
  • 3
  • 22
  • 12
111
votes
4 answers

SQLAlchemy classes across files

I'm trying to figure out how to have SQLAlchemy classes spread across several files, and I can for my life not figure out how to do it. I am pretty new to SQLAlchemy so forgive me if this question is trivial.. Consider these 3 classes in each their…
joveha
  • 2,599
  • 2
  • 17
  • 19
108
votes
12 answers

SQLAlchemy support of Postgres Schemas

We host a multitenant app with SQLAlchemy and postgres. I am looking at moving from having separate databases for each tenant to a single database with multiple schemas. Does SQLAlchemy support this natively? I basically just want every query that…
eleddy
  • 1,354
  • 2
  • 9
  • 8
106
votes
7 answers

How to create an SQL View with SQLAlchemy?

Is there a "Pythonic" way (I mean, no "pure SQL" query) to define an SQL view with SQLAlchemy?
Thibaut D.
  • 2,521
  • 5
  • 22
  • 33
106
votes
4 answers

How to get last record

I need to get last record from db. I'm using sqlalchemy. At the moment, I'm doing like that: obj = ObjectRes.query.all() return str(obj[-1].id) But it's too heavy query. How can I get last record better?
Dmitrijs Zubriks
  • 2,696
  • 6
  • 22
  • 33