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
9
votes
1 answer

Lazy loading on column_property in SQLAlchemy

Say I have the following models: class Department(Base): __tablename__ = 'departments' id = Column(Integer, primary_key=True) class Employee(Base): __tablename__ = 'employees' id = Column(Integer, primary_key=True) department_id…
Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
9
votes
2 answers

TypeError: Invalid argument(s) 'pool_size' sent to create_engine() when using flask_sqlalchemy

I'm using SQLAlchemy==1.0.9 and Flask-SQLAlchemy==2.1 in my Flask application and want to connect to a sqlite db. I get the error TypeError: Invalid argument(s) 'pool_size' sent to create_engine(), using configuration…
Joern Boegeholz
  • 533
  • 1
  • 8
  • 25
9
votes
0 answers

Python and SQLAlchemy: How to detect external changes on database

Some devices are asynchronously storing values on a common remote MySQL database server. I would like to write a supervisor app in Python (and possibly SQLAlchemy) to recognize the external INSERT events on the database and act upon the last rows'…
Alex Poca
  • 2,406
  • 4
  • 25
  • 47
9
votes
1 answer

"This session is in 'prepared' state; no further" error with SQLAlchemy using scoped_session in threaded mod_wsgi app

I recently updated to SQLAlchemy 1.1, which I'm using under Django 1.10 (also recently updated from 1.6), and I keep getting sqlalchemy/mysql errors that This session is in 'prepared' state; no further SQL can be emitted within this transaction. How…
Adam Morris
  • 8,265
  • 12
  • 45
  • 68
9
votes
1 answer

Automagically propagating deletion when using a bidirectional association_proxy

I'm using a bidirectional association_proxy to associate properties Group.members and User.groups. I'm having issues with removing a member from Group.members. In particular, Group.members.remove will successfully remove an entry from Group.members,…
mickeyh
  • 93
  • 5
9
votes
1 answer

Convert datetime to unix timestamp in SQLAlchemy model before executing query?

I am using SQLAlchemy to work with a remote database that uses a strange timestamp format--it stores timestamps as double-precision milliseconds since epoch. I'd like to work with python datetime objects, so I wrote getter/setter methods in my…
nrlakin
  • 5,234
  • 3
  • 16
  • 27
9
votes
1 answer

Dynamic radio buttons from database query using Flask and WTForms

I have experience with PHP but im new to Flask and Python and I need help with a simple thing. I have a database table and I need each entry in a radio button. I need to use WTForms but I can't do it. The id should be the value and the botname…
Azurelle
  • 93
  • 1
  • 6
9
votes
1 answer

How to set common prefix for all tables in SQLAlchemy

I know there is a way to instrument SQLAlchemy to prepend common prefix to all columns, is it possible to add a common prefix to all table names derived from single declarative_base?
canni
  • 5,737
  • 9
  • 46
  • 68
9
votes
1 answer

SqlAlchemy Relationship and Marshmallow

I am trying to return JSON or even a complete string of a returned one to many sqlalchemy query. I am using Marshmallow at this point to try do it but it keeps returning incomplete data I have two models defined as : class UserModel(db.Model): …
Zee18
  • 425
  • 2
  • 5
  • 11
9
votes
2 answers

Commit changes for only one SQLAlchemy model instance when multiple have changed

I queried and changed several instances. I only want to commit the changes to one of them. However, all the changes are committed when I call db.session.commit(). Is there a way to save an object individually, object.save(), like Rails or…
Vic
  • 163
  • 1
  • 8
9
votes
1 answer

Flask-SQLAlchemy - on the fly connections to multiple databases

I have a flask webapp where users will be able to connect to their own mysql databases and query their own tables What's the best way to create multiple connections (to different databases) using flask-sqlalchemy. It seems like it needs to be done…
Shankar ARUL
  • 12,642
  • 11
  • 68
  • 69
9
votes
2 answers

Alembic default value to be used in add_column

How do I get alembic to use a specified default value for a new column, without making it the server_default? Only the existing rows should receive this default value. New rows inserted after should still get server_default.
00500005
  • 3,727
  • 3
  • 31
  • 38
9
votes
2 answers

In SQLAlchemy, how do I query composite primary keys?

I'm using SQLAlchemy to programmatically query a table with a composite foreign key. e.g.: CREATE TABLE example ( id INT NOT NULL, date TIMESTAMP NOT NULL, data VARCHAR(128) PRIMARY KEY (id, date) ) I'd like to take a list of values and get rows…
DaveA
  • 1,227
  • 2
  • 14
  • 19
9
votes
2 answers

How to use sum and order by in SQLAlchemy query

I'm trying to return a sum row from my table and order with the sum result. My sql like this: self.db_user_online.query( MeleeGameData, func.sum(MeleeGameData.core_data).label("ct") ).\ group_by(MeleeGameData.ccid).\ …
steve
  • 1,358
  • 5
  • 16
  • 26
9
votes
1 answer

SQLAlchemy query.filter returned no FROM clauses due to auto-correlation

I'm a beginner SQLAlchemy user frustrated with the extensive documentation. I have a newsfeed that is updated when a new revision is made to some content object. content always has at least one revision. content is related to 1 or more topics…
wheresmycookie
  • 683
  • 3
  • 16
  • 39