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
2 answers

SQLAlchemy: query custom property based on table field

I'm using SQLAlchemy declarative base to define my model. I defined a property name that is computed from one the columns (title): class Entry(Base): __tablename__ = "blog_entry" id = Column(Integer, primary_key=True) title =…
amercader
  • 4,490
  • 2
  • 24
  • 26
9
votes
1 answer

SqlAlchemy and Multiprocessing

I use SqlAlchemy to connect to my database backend and make heavy use of multiprocessing in my Python application. I came to a situation which requires to pass an object reference, which is the result of a database query, from one process to…
Manuel Faux
  • 2,317
  • 5
  • 24
  • 35
9
votes
1 answer

Does closing a SQLAlchemy ORM Session roll back uncommitted changes?

For example, is there a difference between the following two? session = Session() # Session is a session maker try: # do some work session.commit() except: session.rollback() finally: session.close() and session = Session() try: …
lfk
  • 2,423
  • 6
  • 29
  • 46
9
votes
1 answer

Querying with joins in sql alchemy and avoiding select all

I would like to know wether there is a way to combine joining two tables, and retrieving the two entities only with their relevant columns. I want to avoid doing a thing such select * after joining two tables and getting only column id from Table A…
JavaSa
  • 5,813
  • 16
  • 71
  • 121
9
votes
1 answer

Sql Alchemy can't cast jsonb to boolean

I have the following value I'm pulling from a model, and trying to cast to a boolean. However, when I run the program I get an error stating `can't cast jsonb type to boolean. The value for the jsonb value here is a boolean, so why can't it cast it?…
Rafa
  • 3,219
  • 4
  • 38
  • 70
9
votes
1 answer

Sqlalchemy enum migration update fails saying does not exist

I have following sqlalchemy model: class Cart(db.Model): __tablename__ = 'carts' #... cart_status = db.Column(db.Enum('confirmed', 'canceled', name='cart_statuses')) Which generates following migration script: """empty message Revision…
sadaf2605
  • 7,332
  • 8
  • 60
  • 103
9
votes
1 answer

SQLAlchemy order_by many to many relationship through association proxy

I have a many to many relationship setup in a Flask app in SQLAlchemy using a Association Object. I then have have assocation proxies setup between the the classes, to give more direct access rather than going via the association object. Here is an…
Mr Alpha
  • 1,813
  • 1
  • 16
  • 26
9
votes
3 answers

SQLAlchemy, prevent duplicate rows

I'm wondering if it's possible to prevent committing duplicates to the database. For example, presume there is a class as follows class Employee(Base): id = Column(Integer, primary_key=True) name = Column(String) If I were to make a series of…
nven
  • 1,047
  • 4
  • 13
  • 22
9
votes
1 answer

Redshift + SQLAlchemy long query hangs

I'm doing something among the lines of: conn_string = "postgresql+pg8000://%s:%s@%s:%d/%s" % (db_user, db_pass, host, port, schema) conn =…
rodrigocf
  • 1,951
  • 13
  • 39
  • 62
9
votes
0 answers

How to handle cross-schema foreign key in sqlalchemy?

I have a following directory structure. ├── alchemy_models │   ├── foo.py │   ├── bar.py │   └── __init__.py # Base in here └── setup.py foo.py class Foo(Base): __tablename__ = 'foos' __table_args__ = {'schema': 'foo'} barid =…
user1685095
  • 5,787
  • 9
  • 51
  • 100
9
votes
1 answer

slqlalchemy UniqueConstraint VS Index(unique=True)

I am using MySQL (running InnoDB), and wrapped the entire thing using sqlalchemy. Now, I would like to generate changes in my database by using (see docs) sqlalchemy_utils.functions.create_database(...) Generally the above function does what it is…
Basti Vagabond
  • 1,458
  • 1
  • 18
  • 26
9
votes
1 answer

Issue with SqlAlchemy - "Parent instance is not bound to a Session; lazy load operation..."

I have a small thrift server in python that I use do some fast lookups. The server queries mysql via SqlAlchemy on the first request and shoves all returned objects into a dictionary so on subsequent requests no DB call is needed. I just get the…
Tony
  • 2,037
  • 3
  • 22
  • 22
9
votes
1 answer

SQLAlchemy AttributeError: 'Query' object has no attribute '_sa_instance_state' when retrieving from database

The problem is trying to retrieve an object with relationships from the database using SQLAlchemy on Pyramid. What I want basically is to create the objects I need to retrieve from the database to complete the data needed for a web page. When I try…
ffuentes
  • 1,042
  • 5
  • 16
  • 36
9
votes
4 answers

Using SQLAlchemy ORM for a non-primary key, unique, auto-incrementing id

When I run the following code, I am expecting the first_name, and last_name to be a composite primary key and for the id to be an autoincrementing index for the row, but not to act as the primary key, as there the information in the rest of the…
James
  • 787
  • 1
  • 7
  • 15
9
votes
1 answer

Python SQLAlchemy get returning ID after inserting

I want to get the ID of the last inserted record after inserting in postgresql using SQLAlchemy. Here is code, insert_record = {list of data} result = connection.execute(tbl_example.insert().returning(tbl_example.c.id), insert_record) print…
rksh
  • 3,920
  • 10
  • 49
  • 68