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

How to filter concated columns with LIKE using SQLAlchemy?

I have a users table with a first_name and a last_name column. I am trying to create a SQLAlchemy query that will perform a like on a concatanation of the two columns, IE full name. Here is an example first_name: Bob last_name: Smith query = "bob…
Jakobovski
  • 3,203
  • 1
  • 31
  • 38
9
votes
2 answers

pandas.read_sql_table producing table not found error

I'm trying to access my Oracle 11g(r2) Express Edition database via python. I specifically want to create a pandas DataFrame from one of the tables, HISTORY_FULLNESS. However, when I try to use the pandas.read_sql_table() function, it gives me a…
PTTHomps
  • 1,477
  • 2
  • 22
  • 38
9
votes
2 answers

Is multi-level polymorphism possible in SQLAlchemy?

Is it possible to have multi-level polymorphism in SQLAlchemy? Here's an example: class Entity(Base): __tablename__ = 'entities' id = Column(Integer, primary_key=True) created_at = Column(DateTime, default=datetime.utcnow,…
Kiran Jonnalagadda
  • 2,606
  • 1
  • 30
  • 29
9
votes
2 answers

How to work with postgres exclusion constraints in alembic

Is there a way to create a table with a postgresql exclusion constraint in Alembic without writing literal SQL? Consider, for example, this table: CREATE TABLE reservation ( during tsrange, EXCLUDE USING gist (during WITH &&) ); Exclusion…
Julian
  • 1,573
  • 2
  • 22
  • 36
9
votes
1 answer

How to use values like Default and Onupdate in flask-SQLAlchemy

My Code, class User(db.Model, UserMixin): uid = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(250), unique=True) password = db.Column(db.String(250)) name = db.Column(db.String(250)) created_on =…
LeoG
  • 683
  • 1
  • 8
  • 22
9
votes
1 answer

Nested SELECT using sqlalchemy

I would like to create a query with nested SELECT using sqlalchemy, but I cannot get the expected result. I of course simplified the following query so it will be easilly understandable for this post. Here is the query I would like to…
Schminitz
  • 314
  • 1
  • 2
  • 13
9
votes
3 answers

Python SQLAlchemy Query using labeled OVER clause with ORM

This other question says how to use the OVER clause on sqlalchemy: Using the OVER window function in SQLAlchemy But how to do that using ORM? I have something like: q = self.session.query(self.entity, func.count().over().label('count_over')) This…
user1538560
  • 429
  • 1
  • 6
  • 16
9
votes
4 answers

What are some strategies for maintaining a common database schema with a team of developers and no DBA?

I'm curious about how others have approached the problem of maintaining and synchronizing database changes across many (10+) developers without a DBA? What I mean, basically, is that if someone wants to make a change to the database, what are some…
Mahmoud Abdelkader
  • 23,011
  • 5
  • 41
  • 54
9
votes
1 answer

SQLAlchemy chaining association proxy for great grandchildren?

I have a four classes like so: Group, Parent, Child, Toy. Group has a parents relationship pointing to Parent Parent has a children relationship pointing to Child Child has a toys relationship pointing to Toy Parent has a toys association_proxy…
Patrick Yan
  • 2,338
  • 5
  • 25
  • 33
9
votes
2 answers

How to write nested conjunctions (OR and AND clauses) in SQLAlchemy?

Does anybody know how to produce nested conjunctions in SQLAlchemy? I have some Python code similar to this: import sqlalchemy as sa a = sa.and_(mytable.col1 > 1, mytable.col2 < 3) b = sa.and_(mytable.col1 < 1, mytable.col4 == None) clause_args =…
Lasma
  • 345
  • 2
  • 5
9
votes
2 answers

SQLALchemy "after_insert" doesn't update target object fields

I have a model (see code below) on which I want to execute a function after an object is inserted that will update one of the object's fields. I'm using the after_insert Mapper Event to do this. I've confirmed that the after_insert properly calls…
Alex Marse
  • 131
  • 1
  • 1
  • 7
9
votes
1 answer

SQLAlchemy connection errors

I'm experiencing some strange bugs which seem to be caused by connections used by Sqlalchemy, which i can't pin down exactly.. i was hoping someone has a clue whats going on here. We're working on a Pyramid (version 1.5b1) and use Sqlalchemy…
Matthijs Blaas
  • 131
  • 1
  • 6
9
votes
1 answer

Using sqlalchemy to query using multiple column where in clause

I'm looking to execute this query using sqlalchemy. SELECT name, age, favorite_color, favorite_food FROM kindergarten_classroom WHERE (favorite_color, favorite_food) IN (('lavender','lentil soup'),('black','carrot…
crunkchitis
  • 718
  • 2
  • 10
  • 19
9
votes
4 answers

How can I convert Sqlalchemy table object to Pandas DataFrame?

Is it possible to convert retrieved SqlAlchemy table object into Pandas DataFrame or do I need to write a particular function for that aim ?
erogol
  • 13,156
  • 33
  • 101
  • 155
9
votes
2 answers

How to make SQLAlchemy use Memcached?

Has anyone joined SQLAlchemy and Memcached together so that results returned by SQLAlchemy where cached in Memcached? At the moment I have no idea how to implement it myself, I am completely lost and looking for help. Thanks, Boda Cydo.
bodacydo
  • 75,521
  • 93
  • 229
  • 319
1 2 3
99
100