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
164
votes
6 answers

How to create a new database using SQLAlchemy?

Using SQLAlchemy, an Engine object is created like this: from sqlalchemy import create_engine engine = create_engine("postgresql://localhost/mydb") Accessing engine fails if the database specified in the argument to create_engine (in this case,…
Anand Chitipothu
  • 4,167
  • 4
  • 24
  • 26
163
votes
17 answers

SQLAlchemy - Getting a list of tables

I couldn't find any information about this in the documentation, but how can I get a list of tables created in SQLAlchemy? I used the class method to create the tables.
sidewinder
  • 3,013
  • 6
  • 24
  • 33
159
votes
4 answers

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. I am looking for a function in SQLAlchemy that limits the number of results returned by a query to a certain number, for example: 5?…
Xar
  • 7,572
  • 19
  • 56
  • 80
154
votes
6 answers

Efficiently updating database using SQLAlchemy ORM

I'm starting a new application and looking at using an ORM -- in particular, SQLAlchemy. Say I've got a column 'foo' in my database and I want to increment it. In straight sqlite, this is easy: db = sqlite3.connect('mydata.sqlitedb') cur =…
John Fouhy
  • 41,203
  • 19
  • 62
  • 77
153
votes
4 answers

SQLAlchemy: how to filter date field?

Here is model: class User(Base): ... birthday = Column(Date, index=True) #in database it's like '1987-01-17' ... I want to filter between two dates, for example to choose all users in interval 18-30 years. How to implement it with…
Vitalii Ponomar
  • 10,686
  • 20
  • 60
  • 88
152
votes
2 answers

SQLAlchemy: Creating vs. Reusing a Session

Just a quick question: SQLAlchemy talks about calling sessionmaker() once but calling the resulting Session() class each time you need to talk to your DB. For me that means the second I would do my first session.add(x) or something similar, I would…
javex
  • 7,198
  • 7
  • 41
  • 60
146
votes
2 answers

Debugging (displaying) SQL command sent to the db by SQLAlchemy

I have an ORM class called Person, which wraps around a person table: After setting up the connection to the db etc, I run the statement: people = session.query(Person).all() The person table does not contain any data (as yet), so when I print the…
morpheous
  • 16,270
  • 32
  • 89
  • 120
146
votes
6 answers

Case Insensitive Flask-SQLAlchemy Query

I'm using Flask-SQLAlchemy to query from a database of users; however, while user = models.User.query.filter_by(username="ganye").first() will return doing user =…
Ganye
  • 2,481
  • 3
  • 16
  • 13
142
votes
15 answers

jsonify a SQLAlchemy result set in Flask

I'm trying to jsonify a SQLAlchemy result set in Flask/Python. The Flask mailing list suggested the following method http://librelist.com/browser//flask/2011/2/16/jsonify-sqlalchemy-pagination-collection-result/#04a0754b63387f87e59dda564bde426e…
mal-wan
  • 4,391
  • 4
  • 26
  • 37
142
votes
6 answers

flask-sqlalchemy or sqlalchemy

I am new in both flask and sqlalchemy, I just start working on a flask app, and I am using sqlalchemy for now. I was wondering if there is any significant benefit I can get from using flask-sqlalchemy vs sqlalchemy. I could not find enough…
Amin
  • 1,883
  • 4
  • 17
  • 22
141
votes
7 answers

SqlAlchemy - Filtering by Relationship Attribute

I don't have much experience with SQLAlchemy and I have a problem, which I can't solve. I tried searching and I tried a lot of code. This is my Class (reduced to the most significant code): class Patient(Base): __tablename__ = 'patients' id…
user1105851
  • 1,413
  • 2
  • 10
  • 4
138
votes
5 answers

sqlalchemy: how to join several tables by one query?

I have the following SQLAlchemy mapped classes: class User(Base): __tablename__ = 'users' email = Column(String, primary_key=True) name = Column(String) class Document(Base): __tablename__ = "documents" name = Column(String,…
barankin
  • 1,853
  • 2
  • 12
  • 16
138
votes
5 answers

Flask-SQLAlchemy how to delete all rows in a single table

How do I delete all rows in a single table using Flask-SQLAlchemy? Looking for something like this: >>> users = models.User.query.all() >>> models.db.session.delete(users) # but it errs out: UnmappedInstanceError: Class '__builtin__.list' is not…
SeanPlusPlus
  • 8,663
  • 18
  • 59
  • 84
135
votes
1 answer

When do I need to use sqlalchemy back_populates?

When I try SQLAlchemy Relation Example following this guide: Basic Relationship Patterns I have this code #!/usr/bin/env python # encoding: utf-8 from sqlalchemy import create_engine from sqlalchemy import Table, Column, Integer, ForeignKey from…
Liqang Liu
  • 1,654
  • 3
  • 12
  • 20
132
votes
11 answers

How to do an upsert with SqlAlchemy?

I have a record that I want to exist in the database if it is not there, and if it is there already (primary key exists) I want the fields to be updated to the current state. This is often called an upsert. The following incomplete code snippet…
Russ
  • 10,835
  • 12
  • 42
  • 57