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
106
votes
4 answers

Flask sqlalchemy many-to-many insert data

I am trying to make a many to many relation here in Flask-SQLAlchemy, but it seems that I don't know how to fill the "many to many identifier database". Could you please help me understand what I am doing wrong and how it is supposed to look? class…
Sigils
  • 2,492
  • 8
  • 24
  • 36
106
votes
10 answers

How to count rows with SELECT COUNT(*) with SQLAlchemy?

I'd like to know if it's possible to generate a SELECT COUNT(*) FROM TABLE statement in SQLAlchemy without explicitly asking for it with execute(). If I use: session.query(table).count() then it generates something like: SELECT count(*) AS count_1…
tiho
  • 6,655
  • 3
  • 31
  • 31
105
votes
4 answers

How to close sqlalchemy connection in MySQL

This is a sample code I'd like to run: for i in range(1,2000): db = create_engine('mysql://root@localhost/test_database') conn = db.connect() #some simple data operations conn.close() db.dispose() Is there a way of running this…
martincho
  • 4,517
  • 7
  • 32
  • 42
105
votes
7 answers

memory-efficient built-in SqlAlchemy iterator/generator?

I have a ~10M record MySQL table that I interface with using SqlAlchemy. I have found that queries on large subsets of this table will consume too much memory even though I thought I was using a built-in generator that intelligently fetched…
Paul
  • 2,973
  • 6
  • 31
  • 40
104
votes
3 answers

How to close a SQLAlchemy session?

Following what we commented in How to close sqlalchemy connection in MySQL, I am checking the connections that SQLAlchemy creates into my database and I cannot manage to close them without exiting from Python. If I run this code in a python console,…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
102
votes
9 answers

Getting random row through SQLAlchemy

How do I select one or more random rows from a table using SQLAlchemy?
cnu
  • 36,135
  • 23
  • 65
  • 63
102
votes
3 answers

Writing a connection string when password contains special characters

I'm using SQLalchemy for a Python project, and I want to have a tidy connection string to access my database. So for example: engine = create_engine('postgresql://user:pass@host/database') The problem is my password contains a sequence of special…
97
votes
3 answers

Connecting postgresql with sqlalchemy

I know this might be really a simple question but I don't know the solution. What is happening here when I try to connect to postgresql? I am self learner in this field of database and programming so please be gentle with me. When I try following…
Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88
95
votes
3 answers

SQLAlchemy, get object not bound to a Session

I am trying to get an collection of objects out of a database and pass it to another process that is not connected to the database. My code looks like the one below but I keep getting: sqlalchemy.exc.UnboundExecutionError: Instance
Sardathrion - against SE abuse
  • 17,269
  • 27
  • 101
  • 156
95
votes
3 answers

Undo last Alembic migration

I created a migration with alembic revision --autogenerate, applied it to my development database with alembic upgrade head, and then realised it wasn't quite what I wanted. How can I revert the migration so that I can tweak it and try again?
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
94
votes
5 answers

Select NULL Values in SQLAlchemy

Here's my (PostgreSQL) table -- test=> create table people (name varchar primary key, marriage_status varchar) ; test=> insert into people values ('Ken', 'married'); test=> insert into people values ('May',…
Jerry
  • 2,497
  • 4
  • 22
  • 31
93
votes
5 answers

Return Pandas dataframe from PostgreSQL query with sqlalchemy

I want to query a PostgreSQL database and return the output as a Pandas dataframe. I created a connection to the database with 'SqlAlchemy': from sqlalchemy import create_engine engine = create_engine('postgresql://user@localhost:5432/mydb') I…
lmart999
  • 6,671
  • 10
  • 29
  • 37
92
votes
2 answers

Example of what SQLAlchemy can do, and Django ORM cannot

I've been doing a lot of research lately into using Pyramid with SQLAlchemy versus keeping a current application in Django. That by itself is an entire debate, but I'm not here to discuss that. What I do want to know is, why is SQLAlchemy…
limasxgoesto0
  • 4,555
  • 8
  • 31
  • 38
91
votes
3 answers

How to use NOT IN clause in sqlalchemy ORM query

how do i convert the following mysql query to sqlalchemy? SELECT * FROM `table_a` ta, `table_b` tb where 1 AND ta.id = tb.id AND ta.id not in (select id from `table_c`) so far i have this for sqlalchemy: query = session.query(table_a,…
chrizonline
  • 4,779
  • 17
  • 62
  • 102
90
votes
2 answers

Concepts of backref and back_populate in SQLalchemy?

Can anyone explain the concepts of these two ideas and how they relate to making relationships between tables? I can't really seem to find anything that explains it clearly and the documentation feels like there's too much jargon to understand in…
BoopityBoppity
  • 929
  • 2
  • 9
  • 11