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
209
votes
8 answers

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres

I'm trying to connect to a Postgres database with SQLAlchemy. I've installed psycopg2. However, I get the error sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres. How do I configure SQLAlchemy to connect to…
Maharsh Gheewala
  • 2,091
  • 2
  • 10
  • 6
208
votes
11 answers

Bulk insert with SQLAlchemy ORM

Is there any way to get SQLAlchemy to do a bulk insert rather than inserting each individual object. i.e., doing: INSERT INTO `foo` (`bar`) VALUES (1), (2), (3) rather than: INSERT INTO `foo` (`bar`) VALUES (1) INSERT INTO `foo` (`bar`) VALUES…
Nick Holden
  • 3,639
  • 3
  • 22
  • 12
206
votes
9 answers

Flask SQLAlchemy query, specify column names

How do I specify the column that I want in my query using a model (it selects all columns by default)? I know how to do this with the sqlalchmey session: session.query(self.col1), but how do I do it with with models? I can't do SomeModel.query().…
Matthew Scragg
  • 4,540
  • 3
  • 19
  • 27
199
votes
8 answers

Strange SQLAlchemy error message: TypeError: 'dict' object does not support indexing

I am using hand crafted SQL to fetch data from a PG database, using SqlAlchemy. I am trying a query which contains the SQL like operator '%' and that seems to throw SqlAlcjhemy through a loop: sql = """ SELECT DISTINCT u.name from user u …
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
187
votes
9 answers

Flask-SQLalchemy update a row's information

How can I update a row's information? For example I'd like to alter the name column of the row that has the id 5.
pocorschi
  • 3,605
  • 5
  • 26
  • 35
183
votes
10 answers

SQLAlchemy: cascade delete

I must be missing something trivial with SQLAlchemy's cascade options because I cannot get a simple cascade delete to operate correctly -- if a parent element is a deleted, the children persist, with null foreign keys. I've put a concise test case…
carl
  • 49,756
  • 17
  • 74
  • 82
181
votes
3 answers

sqlalchemy IS NOT NULL select

How can I add the filter as in SQL to select values that are NOT NULL from a certain column ? SELECT * FROM table WHERE YourColumn IS NOT NULL; How can I do the same with SQLAlchemy filters? select =…
salamey
  • 3,633
  • 10
  • 38
  • 71
180
votes
10 answers

sqlalchemy flush() and get inserted id?

I want to do something like this: f = Foo(bar='x') session.add(f) session.flush() # do additional queries using f.id before commit() print f.id # should be not None session.commit() But f.id is None when I try it. How can I get this to work?
Eloff
  • 20,828
  • 17
  • 83
  • 112
178
votes
8 answers

How to write DataFrame to postgres table

There is DataFrame.to_sql method, but it works only for mysql, sqlite and oracle databases. I cant pass to this method postgres connection or sqlalchemy engine.
m9_psy
  • 3,217
  • 5
  • 25
  • 38
176
votes
13 answers

How do I get a raw, compiled SQL query from a SQLAlchemy expression?

I have a SQLAlchemy query object and want to get the text of the compiled SQL statement, with all its parameters bound (e.g. no %s or other variables waiting to be bound by the statement compiler or MySQLdb dialect engine, etc). Calling str() on the…
cce
  • 4,874
  • 2
  • 28
  • 25
174
votes
15 answers

SQLAlchemy ORM conversion to pandas DataFrame

Is there a solution converting a SQLAlchemy to a pandas DataFrame? Pandas has the capability to use pandas.read_sql but this requires use of raw SQL. I have two reasons for wanting to avoid it: I already have everything using the ORM…
Jared
  • 3,651
  • 11
  • 39
  • 64
172
votes
3 answers

Group by & count function in sqlalchemy

I want a "group by and count" command in sqlalchemy. How can I do this?
Nazmul Hasan
  • 6,840
  • 13
  • 36
  • 37
169
votes
16 answers

ImportError: No module named MySQLdb

I am referring the following tutorial to make a login page for my web application. http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982 I am having issue with the database. I am getting an ImportError: No module named…
user3182194
  • 1,729
  • 2
  • 13
  • 9
165
votes
3 answers

How do I execute inserts and updates in an Alembic upgrade script?

I need to alter data during an Alembic upgrade. I currently have a 'players' table in a first revision: def upgrade(): op.create_table('player', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name',…
Arek S
  • 4,239
  • 4
  • 24
  • 33
165
votes
11 answers

How can I use UUIDs in SQLAlchemy?

Is there a way to define a column (primary key) as a UUID in SQLAlchemy if using PostgreSQL (Postgres)?
Vasil
  • 36,468
  • 26
  • 90
  • 114