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
656
votes
7 answers

SQLAlchemy: What's the difference between flush() and commit()?

What the difference is between flush() and commit() in SQLAlchemy? I've read the docs, but am none the wiser - they seem to assume a pre-understanding that I don't have. I'm particularly interested in their impact on memory usage. I'm loading some…
AP257
  • 89,519
  • 86
  • 202
  • 261
635
votes
6 answers

SQLAlchemy ORDER BY DESCENDING?

How can I use ORDER BY descending in a SQLAlchemy query like the following? This query works, but returns them in ascending order: query = (model.Session.query(model.Entry) .join(model.ClassificationItem) …
AP257
  • 89,519
  • 86
  • 202
  • 261
411
votes
5 answers

Difference between filter and filter_by in SQLAlchemy

Could anyone explain the difference between filter and filter_by functions in SQLAlchemy? Which one should I be using?
bodacydo
  • 75,521
  • 93
  • 229
  • 319
407
votes
44 answers

How to convert SQLAlchemy row object to a Python dict?

Is there a simple way to iterate over column name and value pairs? My version of SQLAlchemy is 0.5.6 Here is the sample code where I tried using dict(row): import sqlalchemy from sqlalchemy import * from sqlalchemy.ext.declarative import…
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
396
votes
8 answers

SQLAlchemy IN clause

I'm trying to do this query in sqlalchemy SELECT id, name FROM user WHERE id IN (123, 456) I would like to bind the list [123, 456] at execution time.
wonzbak
  • 7,734
  • 6
  • 26
  • 25
330
votes
10 answers

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…
starwing123
  • 3,413
  • 3
  • 13
  • 7
300
votes
37 answers

How to serialize SqlAlchemy result to JSON?

Django has some good automatic serialization of ORM models returned from DB to JSON format. How to serialize SQLAlchemy query result to JSON format? I tried jsonpickle.encode but it encodes query object itself. I tried json.dumps(items) but it…
Zelid
  • 6,905
  • 11
  • 52
  • 76
300
votes
11 answers

SQLAlchemy default DateTime

This is my declarative model: import datetime from sqlalchemy import Column, Integer, DateTime from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Test(Base): __tablename__ = 'test' id = Column(Integer,…
Brandon O'Rourke
  • 24,165
  • 16
  • 57
  • 58
298
votes
6 answers

Using OR in SQLAlchemy

I've looked through the docs and I cant seem to find out how to do an OR query in SQLAlchemy. I just want to do this query. SELECT address FROM addressbook WHERE city='boston' AND (lastname='bulger' OR firstname='whitey') Should be something…
JiminyCricket
  • 7,050
  • 7
  • 42
  • 59
287
votes
11 answers

SQLAlchemy: print the actual query

I'd really like to be able to print out valid SQL for my application, including values, rather than bind parameters, but it's not obvious how to do this in SQLAlchemy (by design, I'm fairly sure). Has anyone solved this problem in a general way?
bukzor
  • 37,539
  • 11
  • 77
  • 111
266
votes
3 answers

SQLAlchemy: engine, connection and session difference

I use SQLAlchemy and there are at least three entities: engine, session and connection, which have execute method, so if I e.g. want to select all records from table I can do this engine.execute(select([table])).fetchall() and…
ololobus
  • 3,568
  • 2
  • 18
  • 23
261
votes
4 answers

sqlalchemy unique across multiple columns

Let's say that I have a class that represents locations. Locations "belong" to customers. Locations are identified by a unicode 10 character code. The "location code" should be unique among the locations for a specific customer. The two below fields…
Ominus
  • 5,501
  • 8
  • 40
  • 44
246
votes
7 answers

How to update SQLAlchemy row entry?

Assume table has three columns: username, password and no_of_logins. When user tries to login, it's checked for an entry with a query like user = User.query.filter_by(username=form.username.data).first() If password matches, he proceeds further.…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
223
votes
4 answers

How to delete a record by id in Flask-SQLAlchemy

I have users table in my MySql database. This table has id, name and age fields. How can I delete some record by id? Now I use the following code: user = User.query.get(id) db.session.delete(user) db.session.commit() But I don't want to make any…
Sergey
  • 5,396
  • 3
  • 26
  • 38
216
votes
11 answers

Does SQLAlchemy have an equivalent of Django's get_or_create?

I want to get an object from the database if it already exists (based on provided parameters) or create it if it does not. Django's get_or_create (or source) does this. Is there an equivalent shortcut in SQLAlchemy? I'm currently writing it out…
FogleBird
  • 74,300
  • 25
  • 125
  • 131
1
2 3
99 100